繁体   English   中英

对多个用户ID的多个关键字搜索在tweepy python中引发错误

[英]Multiple keyword search on multiple user Ids throwing an error in tweepy python

我正在尝试跟踪用户列表,并跟踪与列表中提到的任何用户发布的任何推文匹配的关键字列表。

import tweepy
import simplejson as json
from bson.json_util import dumps
consumer_key = "ABC"
consumer_secret = "ABC"
access_token = "ABC"
access_secret = "ABC"

    # Create a tweepy Authorization object with your twitter keys
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
        decoded = json.loads(data)

        #Also, we convert UTF-8 to ASCII ignoring all bad characters sent by users
        print '@%s: %s' % (decoded['user']['screen_name'], decoded['text'].encode('ascii', 'ignore'))
        print ''
        return True

        try:
            print "%s\t%s\t%s\t%s" % (status.text, 
                                      status.author.screen_name, 
                                      status.created_at, 
                                      status.source,)
        except Exception, e:
            print error
    def filter(self, follow=None,  track=None,  async=False,  locations=None):
            self.parameters = {}
            self.headers['Content-type'] = "application/x-www-form-urlencoded"
            if self.running:
                raise TweepError('Stream object already connected!')
            self.url = '/%i/statuses/filter.json?delimited=length' % STREAM_VERSION
            if follow:
                self.parameters['follow'] = ' '.join(map(str,  follow))
            if track:
                self.parameters['track'] = ' '.join(map(str,  track))
            if locations and len(locations) > 0:
                assert len(locations) % 4 == 0
                self.parameters['locations'] = ' '.join('%.2f' % l for l in locations)
                self.body = urllib.urlencode(self.parameters)
                self.parameters['delimited'] = 'length'
                self._start(async)


    def on_error(self, status_code):
        return True 
stream = tweepy.Stream(auth, CustomStreamListener)
users = [17006157,59145948,157009365]
stream.filter(follow=users, languages=["en"])
keywords = ["crude oil", "robotics", "social unrest"] 
stream.filter(track=keywords, languages=["en"])

运行上面的代码时,我得到了回溯。 我是python的新手,所以如果我问过任何愚蠢的问题,我的歉意是

Traceback (most recent call last):
  File "C:\Python27\nytimes\10062014\Tweepyeditedcode.py", line 86, in <module>
    stream.filter(follow=users, languages=["en"])
  File "build\bdist.win32\egg\tweepy\streaming.py", line 296, in filter
    encoded_follow = [s.encode(encoding) for s in follow]
AttributeError: 'int' object has no attribute 'encode'

我已经对代码进行了与您提到的相同的更改,但是我遇到了一个新错误,然后@confuser

Traceback (most recent call last):
  File "C:\Python27\nytimes\10062014\Tweepyeditedcode.py", line 87, in <module>
    stream.filter(follow=users, languages=["en"])
  File "build\bdist.win32\egg\tweepy\streaming.py", line 313, in filter
    self._start(async)
  File "build\bdist.win32\egg\tweepy\streaming.py", line 235, in _start
    self._run()
  File "build\bdist.win32\egg\tweepy\streaming.py", line 192, in _run
    self.listener.on_exception(exception)
TypeError: unbound method on_exception() must be called with CustomStreamListener instance as first argument (got TypeError instance instead)

请帮助我解决问题。

我最初的想法是Streaming API需要一个用户名列表,而不是ID,但是看起来Twitter文档确认了他们想要的ID:

https://dev.twitter.com/docs/streaming-apis/parameters#follow

也许首先尝试将id转换为字符串?

stream = tweepy.Stream(auth, CustomStreamListener)
users = [17006157,59145948,157009365]
users = [str(u) for u in users]
stream.filter(follow=users, languages=["en"])

更新:

关于新问题,您可以尝试将两个参数( follow=userstrack=keywords )组合到单个对filter函数的调用中:

stream = tweepy.Stream(auth, CustomStreamListener)
users = ['17006157', '59145948', '157009365' ]
keywords = ["crude oil", "robotics", "social unrest"] 
stream.filter(track=keywords, follow=users, languages=["en"])

暂无
暂无

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

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