简体   繁体   English

使用tweepy从特定用户那里获取关键字

[英]Using tweepy to get a keyword from a specific user

I'm trying to create a listener to a very specific twitter account (mine), so I can do some automation, if I tweet something with a "special" code at the end (could be a character like "…") it will trigger an action, like adding the previous characters to a database. 我正在尝试为一个非常特定的twitter帐户(我的)创建一个侦听器,所以我可以做一些自动化操作,如果我在末尾添加带有“特殊”代码的内容(可能是类似“ ...”的字符),它将触发操作,例如将以前的字符添加到数据库中。

So, I used Tweepy and I'm able to create the listener, filter keywords and so, but it will filter keywords from all the Tweetverse. 因此,我使用了Tweepy,并且能够创建侦听器,过滤关键字等,但是它将过滤所有Tweetverse中的关键字。 This is my code: 这是我的代码:

import tweepy

cfg = { 
    "consumer_key"        : "...",
    "consumer_secret"     : "...",
    "access_token"        : "...",
    "access_token_secret" : "..." 
    }

auth = tweepy.OAuthHandler(cfg['consumer_key'], cfg['consumer_secret'])
auth.set_access_token(cfg['access_token'], cfg['access_token_secret'])
api = tweepy.API(auth)

class MyStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print(status.text)
        return True

    def on_error(self, status):
        print('error ',status)
        return False

myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth=auth, listener=myStreamListener)

myStream.filter(track=['…'])

It will filter all the messages containing a "…" no matter who wrote it, so I added to the last line the parameter follow='' like: 不管是谁写的,它将过滤所有包含“…”的消息,因此我在最后一行添加了参数follow=''例如:

myStream.filter(follow='myTwitterName', track=['…'])

It always gives me a 406 error, if I use myStream.userstream('myTwitterName') it will give me, not just the Tweets I write, but also my whole timeline. 它总是给我一个406错误,如果我使用myStream.userstream('myTwitterName')它不仅会给我写的Tweets,而且会给我整个时间轴。

So, what am I doing wrong? 那么,我在做什么错呢?

EDIT 编辑

I just find my first error. 我只是发现我的第一个错误。 I was using user's screen name, not Twitter ID. 我使用的是用户的屏幕名称,而不是Twitter ID。 Now I got rid of the 406 error, but still doesn't work. 现在,我摆脱了406错误,但仍然无法正常工作。 I placed the Twitter ID in the follow parameter, but does absolutely nothing. 我将Twitter ID放置在follow参数中,但绝对不执行任何操作。 I tried both, with my account and with an account that is too "live", like CNN (ID = 759251), I see new tweets coming in my browser, but nothing on the listener. 我使用自己的帐户和过于“活跃”的帐户(例如CNN(ID = 759251))尝试了这两种方法,但我在浏览器中看到了新的推文,但侦听器上却什么也没有。

If you're interested on knowing your own Twitter ID, I used this service: http://gettwitterid.com/ 如果您想知道自己的Twitter ID,可以使用以下服务: http : //gettwitterid.com/

OK, solved. 好,解决了 It was working from the very beggining, I made two mistakes: 从一开始就在工作,我犯了两个错误:

  • To solve the 406 error all it has to be done, is to use Twitter id instead of Twitter name. 要解决406错误,只需使用Twitter ID而不是Twitter名称即可解决。

  • The listener was apparently doing nothing, because I was sending "big" tweets, that is, tweets longer than 140 chars. 收听者显然没有执行任何操作,因为我发送的是“大”推文,即长于140个字符的推文。 In this case, you shouldn't use status.text , but status.extended_tweet['full_text'] 在这种情况下,你不应该使用status.text ,但status.extended_tweet['full_text']

  • You must check for the existance of the extended_tweet , if it is not in the status received, then you should use the text 您必须检查extended_tweet的存在,如果它不处于接收状态,则应使用text

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

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