D. *_*Wei 1 python twitter stream tweepy python-3.x
我正在尝试使用这些典型代码来积累推文数据。如您所见,我尝试跟踪包含“UniversalStudios”、“Disneyland”或“Los Angeles”的推文。但事实上,我真正想要的是包含这些关键词“UniversalStudios”、“迪士尼乐园”和“洛杉矶”的推文。谁能告诉我如何实现这一目标?
非常感谢提前:)
#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
all_data = json.loads(data)
tweet = TextBlob(all_data["text"])
#Add the 'sentiment data to all_data
#all_data['sentiment'] = tweet.sentiment
#print(tweet)
#print(tweet.sentiment)
# Open json text file to save the tweets
with open('tweets.json', 'a') as tf:
# Write a new line
tf.write('\n')
# Write the json data directly to the file
json.dump(all_data, tf)
# Alternatively: tf.write(json.dumps(all_data))
return True
def on_error(self, status):
print (status)
if __name__ == '__main__':
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
#This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
stream.filter(languages = ['en'], track=['UniversalStudios','Disneyland', "LosAngeles"])
Run Code Online (Sandbox Code Playgroud)
Twitter 的 API(参见“track”)提到你需要在短语之间有空格来表示 AND(逗号是 OR)。我不确定您使用的库如何处理它,但我敢打赌:
track=['UniversalStudios Disneyland LosAngeles']
Run Code Online (Sandbox Code Playgroud)
来自文档的引用:
通过此模型,您可以将逗号视为逻辑 OR,而空格等效于逻辑 AND(例如,'the twitter' 是 AND 推特,而 'the,twitter' 是 OR 推特)。
| 归档时间: |
|
| 查看次数: |
3390 次 |
| 最近记录: |