【发布时间】:2021-10-26 05:50:46
【问题描述】:
我现在正在从事自然语言处理项目,但我一开始就无法收集特定语言的推文。
我正在尝试将 tweepy 库与 python 一起使用,但此代码在控制台上没有任何回报
我哪里做错了?
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
import json
# authentication data- get this info from twitter after you create your application
ckey="*****"
csecret="*****"
atoken="*****"
asecret="*****"
# define listener class
class listener(StreamListener):
def on_data(self, data):
try:
print (data) # write the whole tweet to terminal
return True
except BaseException as e:
print('failed on data, ', str(e)) # if there is an error, show what it is
time.sleep(5) # one error could be that you're rate-limited; this will cause the script to pause for 5 seconds
def on_error(self, status):
print (status)
# authenticate yourself
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(languages=['tr']) # track what you want to search for!
【问题讨论】:
-
作为建议,即使它们是模拟密钥,也不要在线发布密钥,因为有人可能会通过使用这些密钥来冒充您