Python,tweepy流
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python,tweepy流相关的知识,希望对你有一定的参考价值。
我使用的代码类似于下面的代码:https://github.com/tweepy/tweepy/blob/master/examples/streaming.py
API允许您跟踪多个过滤条件,在此示例中为track = ['usa','canada']。这实际上意味着该流将收集提及“加拿大”或“美国”的推文。
问题是函数on_data()打印数据,但它没有指定数据属于哪个过滤器术语。当你只过滤一个术语时,例如在github页面上提供的样本中,那么它是隐含的,但是当你有多个术语时,你如何打印术语和与之相关的数据?
换句话说,我如何知道哪些推文被“加拿大”过滤而哪些被“美国”过滤?
from __future__ import absolute_import, print_function
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
# Go to http://apps.twitter.com and create an app.
# The consumer key and secret will be generated for you after
consumer_key=""
consumer_secret=""
# After the step above, you will be redirected to your app's page.
# Create an access token under the the "Your access token" section
access_token=""
access_token_secret=""
class StdOutListener(StreamListener):
""" A listener handles tweets that are received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
def on_data(self, data):
print(data)
return True
def on_error(self, status):
print(status)
if __name__ == '__main__':
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
stream.filter(track=['usa','canada'])
答案
你没有提到第三种可能性:一条推文与“加拿大”和“美国”相匹配。不过,解决方案只是测试推文中是否存在任何一个或两个过滤词。所以:
def on_data(self, data):
text = data.text.lower()
if "canada" in text:
do_canada()
if "usa" in text:
do_usa()
return True
以上是关于Python,tweepy流的主要内容,如果未能解决你的问题,请参考以下文章