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

时间:2020-12-03 15:35:42

I'm using below code to streaming tweets and analyse them for making decisions. while running the below code I got an error. that error occurs twitter users those who had the friend list of more than 50.

我正在使用下面的代码来传输推文并分析它们以做出决策。在运行以下代码时出现错误。这个错误发生在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)

while executing this script I have got an error as below.

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

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

I have googled a lot.but nothing worked. Can somebody help me to solve the problem?

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

1 个解决方案

#1


0  

401 is an http status code for 'Unauthorized'. I would suggest verifying your credentials.

401是“未授权”的http状态代码。我建议您验证您的凭据。

#1


0  

401 is an http status code for 'Unauthorized'. I would suggest verifying your credentials.

401是“未授权”的http状态代码。我建议您验证您的凭据。