我想用python來計算我的臉書頁面喜歡,但我得到以下錯誤。如何修復typeError'int'對象沒有屬性'__getitem__'?
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__'
有這個幾個相關的職位,但我無法弄清楚
這是我使用的代碼。
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)
有人可以幫我解決這個問題嗎?
'page_data'是一個int,從函數 –
返回的錯誤的代碼還行。你有解決方案嗎? –
如果page_dara不符合您的期望,您需要返回並重新考慮您的代碼。 – Pureferret