大家。我是社交媒体信息挖掘的新手。所以当我运行下面的脚本并获得“401”的结果时,我很困惑,我用Google搜索我需要授权。请有人帮帮我,非常感谢你。
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
import json
consumer_key = "XXX"
consumer_secret = " XX"
access_token = "XX"
access_secret = "XX"
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
def parse(cls, api, raw):
status = cls.first_parse(api, raw)
setattr(status, 'json', json.dumps(raw))
return status
tweepy.models.Status.first_parse = tweepy.models.Status.parse
tweepy.models.Status.parse = parse
class MyListener(tweepy.StreamListener):
def on_data(self, data):
try:
with open('FILENAME.json', 'a') as f:
f.write(data)
return True
except BaseException as e:
print("Error on_data: %s" % str(e))
return True
def on_error(self, status):
print(status)
return True
twitter_stream = Stream(auth, MyListener())
twitter_stream.filter(track=['#HASHTAG_TO_SEARCH'])
答案 0 :(得分:0)
我用自己的密钥和秘密运行你的代码,它就像魅力一样。您可能必须在Twitter的应用程序管理中重新生成您的消费者密钥和密码,然后重试。