简体   繁体   English

tweepy的Twitter扫描仪-Python

[英]Twitter Scanner w tweepy - Python

I was just wondering if it's possible to make a scanner with tweepy - for instance, a while loop that is constantly searching for certain words. 我只是想知道是否有可能使扫描仪具有扭曲感-例如,一个while循环不断搜索某些单词。 I'm a trader and would find it very useful in case there is any breaking news. 我是一名交易员,如果有任何重大新闻,它会非常有用。

Example: 例:

I want to set my scanner to constantly return tweets that have '$DB' in them. 我想将扫描仪设置为不断返回其中包含“ $ DB”的推文。 Furthermore, I only want to return tweets of users that have > 5k followers. 此外,我只想返回关注者超过5k的用户的推文。

Any advice or pointers would be helpful! 任何建议或指示都将有所帮助! Thanks. 谢谢。

Edit/Update: As discussed by asongtoruin and qorka, the question asks for new tweets, not existing tweets. 编辑/更新:正如asongtoruin和qorka所讨论的,该问题要求提供新的tweet,而不是现有的tweet。 Previous edit used api.search method which finds only existing messages. 先前的编辑使用了api.search方法,该方法仅查找现有消息。 The StreamListener reads new messages. StreamListener读取新消息。

import tweepy
from tweepy import OAuthHandler

access_token='your_api_token'
access_secret='your_api_access_secret'
consumer_key = 'your_api_key'
consumer_secret = 'your_consumer_key'

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

api = tweepy.API(auth)

class MyListener(StreamListener):
    def on_status(self, status):
        try:
            if status.user.followers_count > 5000:
                print '%s (%s at %s, followers: %d)' % (status.text, status.user.screen_name, status.created_at, status.user.followers_count)
                return True
        except BaseException as e:
            print("Error on_status: %s" % str(e))
        return True

    def on_error(self, status):
        print(status)
        return True

twitter_stream = Stream(auth, MyListener())
twitter_stream.filter(track=['$DB','$MS','$C'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM