由于tweepy错误401,python脚本执行失败

时间:2018-04-11 04:23:35

标签: python python-3.x twitter twitter-oauth tweepy

我正在使用下面的代码来传输推文并分析它们以做出决策。在运行以下代码时出现错误。这个错误发生在Twitter用户那些朋友列表超过50的人。

import re
import tweepy
import sys
import time

consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
users = tweepy.Cursor(api.friends, screen_name='@myuser').items()

while True:
    try:
       user = next(users)
    except tweepy.TweepError:
       time.sleep(60*15)
       user = next(users)
    except StopIteration:
       break
    for status in tweepy.Cursor(api.user_timeline,screen_name=user.screen_name,result_type='recent').items(5):
            text=status._json['text'].translate(non_bmp_map)
            print (user.screen_name + '  >>>>>> '+text)

执行此脚本时,我收到如下错误。

Traceback (most recent call last):
File "D:sensitive2demo.py", line 31, in <module>
for status in tweepy.Cursor(api.user_timeline,screen_name=user.screen_name,result_type='recent').items(5):
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\cursor.py", line 49, in __next__
return self.next()
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\cursor.py", line 197, in next
self.current_page = self.page_iterator.next()
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\cursor.py", line 108, in next
data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\binder.py", line 250, in _call
return method.execute()
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\tweepy-3.6.0-py3.6.egg\tweepy\binder.py", line 234, in execute
raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: Twitter error response: status code = 401

我搜索了很多内容。但没有任何效果。有人可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:0)

401是&#39; Unauthorized&#39;的http状态代码。我建议验证你的证书。