0

I am using JSON library and trying to import a page feed to an CSV file. Tried many a ways to get the result however every time code execute it Gives JSON not serialzable. No Facebook use auth code which I have and used it so connection string will change however if you use a page which has public privacy you will still be able to get the result from below code.

following is the code

import urllib3
import json
import requests
#from pprint import pprint
import csv
from urllib.request import urlopen

page_id = "abcd" # username or id
api_endpoint = "https://graph.facebook.com"
fb_graph_url = api_endpoint+"/"+page_id
try:
#api_request = urllib3.Requests(fb_graph_url)
    #http = urllib3.PoolManager()
    #api_response = http.request('GET', fb_graph_url)
    api_response = requests.get(fb_graph_url)
    try:
        #print (list.sort(json.loads(api_response.read())))
        obj = open('data', 'w')
        #    write(json_dat)
        f = api_response.content

        obj.write(json.dumps(f))
        obj.close()
    except Exception as ee:
        print(ee)
except Exception as e:
    print( e)

Tried many approach but not successful. hope some one can help

1
  • You are getting the webpage source not json Commented Jun 19, 2015 at 15:08

2 Answers 2

1

api_response.content is the text content of the API, not a Python object so you won't be able to dump it.

Try either:

f = api_response.content
obj.write(f)

Or

f = api_response.json()
obj.write(json.dumps(f))
Sign up to request clarification or add additional context in comments.

2 Comments

Tried the Solutions provided however was getting must be str, not bytes and 'bytes' object has no attribute 'json' no resolution
Can you open an interactive session and print the content of api_response.content?
0
requests.get(fb_graph_url).content

is probably a string. Using json.dumps on it won't work. This function expects a list or a dictionary as the argument.

If the request already returns JSON, just write it to the file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.