facebook - Why am I getting KeyError in the following python code? -


i wanna execute code.

this error i'm getting in terminal after running code

traceback (most recent call last):   file "/private/var/folders/6j/n37bd5r92sj8wfkn3k_k9k580000gp/t/cleanup @ startup/bday-459934533.707.py", line 68, in <module>     print 'total='+str(process_posts(url))    file "/private/var/folders/6j/n37bd5r92sj8wfkn3k_k9k580000gp/t/cleanup @ startup/bday-459934533.707.py", line 32, in process_posts     posts=res_obj["data"]  keyerror: 'data'  

the code -

import httplib, urllib  bs4 import beautifulsoup  import os  import json  import time  import calendar  access_token='value' dob='2015-07-30' conn = httplib.httpsconnection("graph.facebook.com")  print 'requesting...' #conn.request("get",path,urllib.urlencode(data),{})  has_more=false def convert_to_local(s):  t=time.strptime(s[:19],"%y-%m-%dt%h:%m:%s")  t=time.localtime(calendar.timegm(t))  t=time.strftime("%y-%m-%d",t)  return t   def getrandomthnx(msg):  return 'thanks :)' def process_posts(url):  conn = httplib.httpsconnection("graph.facebook.com")  conn.request("get",url)  res = conn.getresponse()  conn.getresponse  data=res.read()  res_obj=json.loads(data)  posts=res_obj["data"]  processed=0 post in posts:              if not "message" in post:          continue     msg=post["message"]      post_date=convert_to_local(post["created_time"])              if dob == post_date:                      if "from" in post , "message" in post:                              user= post["from"]["name"]             path='/'+post['id']+'/comments'             param_data={  'format':'json',                      'message':getrandomthnx(msg),                      'access_token':access_token                    }              conn = httplib.httpsconnection("graph.facebook.com")              if post["comments"]["count"]==0:                  print 'responding :'+user+'->'+msg                  conn.request("post",path,urllib.urlencode(param_data),{})                  res = conn.getresponse()              path='/'+post['id']+'/likes'             param_data={  'format':'json',                      'access_token':access_token                    }              conn = httplib.httpsconnection("graph.facebook.com")              processed+=1 if "paging" in res_obj:              return processed+process_posts(res_obj["paging"]["next"]      [len("https://graph.facebook.com"):])  else:      print "finished"     return processed      url='/me/feed?access_token='+access_token  print 'total='+str(process_posts(url))  print 'thanx wisher :)' 

it means data json receive in response query doesn't contain "data" key.

you can visualise how json data looks doing like:

import httplib, urllib  bs4 import beautifulsoup  import os  import json  import time  import calendar  access_token='value' dob='2015-07-30' conn = httplib.httpsconnection("graph.facebook.com")  print 'requesting...' #conn.request("get",path,urllib.urlencode(data),{})  has_more=false def convert_to_local(s):      t=time.strptime(s[:19],"%y-%m-%dt%h:%m:%s")      t=time.localtime(calendar.timegm(t))      t=time.strftime("%y-%m-%d",t)      return t   def getrandomthnx(msg):      return 'thanks :)'  def process_posts(url):       conn = httplib.httpsconnection("graph.facebook.com")      conn.request("get",url)      res = conn.getresponse()      conn.getresponse      data=res.read()      res_obj=json.loads(data)      try:         posts=res_obj["data"]     except:         print "res_obj not contain 'data', here res_obj looks like:"         print data   ... 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -