how to check if a field exist in facebook graph api using python? -
as facebook graph api uses json format show data.i want books name liked friends using python , facebook graph api program stops when of friend have not read book. please give me way check if field exist or not.thanks in advance.
here code :
print name of friends , books liked them. id , access token given user.
import requests import json
base_url = raw_input("enter id : ")
access_token = raw_input("paste here access token : ")
base_url = '6365047697172' access_token = 'caacedeose0cbancfk1kuosp6gzcosmqzc17v1vzcmwra5fw16pks7iuoz2zailizcfnij32rzcvzb7kah0aa6dqludloyzbi6gs30vlzay2jp7zczm0tpbcnermkrpdt2pifxxdscnbzasskypikuxsppkksncemkeugbzb3upxniy4bw0dmkkqeuhqrns5xuxcdynevtyh17pzb09lgzboipw2' base_url = 'https://graph.facebook.com/' + base_url
fields ='friends{books,name}' url = '%s?fields=%s&access_token=%s' % \ (base_url, fields, access_token,)
def get_books(): """ returns list of posts on timeline """
parameters = {'access_token': access_token} r = requests.get(url) result = json.loads(r.text) friends = result["friends"] frnd_data = friends["data"] frnd_name in frnd_data: print frnd_name['name'] frnd_book = frnd_name['books'] #error when there no field name books f_b_name = frnd_book["data"] b_name in f_b_name: print b_name['name']
get_books()
if use json module in python, method load (or loads) maps json dictionary. can treat dictionary means can ask if contains value using following code
if field in json: ...
i hope helps.
Comments
Post a Comment