执行Twitter脚本后3分钟出现奇怪的Python错误?
在运行一个抓取历史推文的Python脚本时,过了大约2到3分钟,我就遇到了以下错误!!!
我真的不知道这是什么意思
也不知道该如何防止这种情况发生
我肯定是在我的请求限制内,因为在每次执行抓取循环之前,我都会检查,结果显示是正确的!!!
谢谢你的帮助
Traceback (most recent call last):
File "twitter.py", line 13, in <module>
openurl = urllib.urlopen("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&contributor_details&include_rts=true&screen_name="+user+"&count=3600")
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 84, in urlopen
return opener.open(url)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 205, in open
return getattr(self, name)(url)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 449, in open_https
return self.http_error(url, fp, errcode, errmsg, headers)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 369, in http_error
result = method(url, fp, errcode, errmsg, headers)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 691, in http_error_401
errcode, errmsg, headers)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 379, in http_error_default
raise IOError, ('http error', errcode, errmsg, headers)
IOError: ('http error', 401, 'Unauthorized', <httplib.HTTPMessage instance at 0x1005ca9e0>)
这个脚本运行得很好,但过了2-3分钟后总是会出问题....
1 个回答
2
Twitter的接口每小时只能查询150次。你可以查看这个链接了解更多信息:https://dev.twitter.com/docs/rate-limiting。如果你想一次查询多条推文,可以试试这样做。我觉得你的401响应中会有解释。试试这个方法:
try:
response = urllib2.urlopen.....
except urllib2.HTTPError as e:
error = e.read() # this will be your error message
print error