[英]How to fix typeError 'int' object has no attribute '__getitem__'?
I want to count my facebook page likes with python but I get the following error. 我想用python来计数我的facebook页面赞,但出现以下错误。
Traceback (most recent call last):
File "facebook_test.py", line 29, in <module>
print "Page Name:"+ page_data['name']
TypeError: 'int' object has no attribute '__getitem__'
There are several related posts about this but I can't figure it out 有几个与此相关的帖子,但我不知道
Here is my used code. 这是我使用的代码。
import urllib2
import json
import time
def get_page_data(page_id,access_token):
api_endpoint = "https://graph.facebook.com/v2.4/"
fb_graph_url = api_endpoint+page_id+"?fields=id,name,likes,unread_notif_count,link&access_token="+access_token
try:
api_request = urllib2.Request(fb_graph_url)
api_response = urllib2.urlopen(api_request)
try:
return json.loads(api_response.read())
except (ValueError, KeyError, TypeError):
return "JSON error"
except IOError, e:
if hasattr(e, 'code'):
return e.code
elif hasattr(e, 'reason'):
return e.reason
while 1:
page_id = "xxxxxxxxxxx" # username or id
token = "XXXXXXXX"
page_data = get_page_data(page_id,token)
print "Page Name:"+ page_data['name']
print "Likes:"+ str(page_data['likes'])
print "Unread notifications:"+ str(page_data['unread_notif_count'])
time.sleep(0.5)
Can someone help me out with this? 有人可以帮我这个忙吗?
You have multiple return points in your get_data
code, returning different data types, but the way you're using it, implied you always expect it to be a dict. 您的get_data
代码中有多个返回点,返回不同的数据类型,但是使用它的方式意味着您始终希望它是一个命令。 At Stone point it returns an interview, when it expects something else. 在Stone点,如果有其他期望,它将返回一次采访。
Either change your get_data
method so it always returns a dict, or preferably, change it so it only returns in one place and one data type (your dict) and let it throw an exception when it fails. 要么更改您的get_data
方法,使其始终返回一个dict,要么更改它,使其仅在一个位置和一种数据类型(您的dict)中返回,并使其在失败时引发异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.