在Tweepy Python中对多个用户ID进行多关键字搜索时出现错误

0 投票
1 回答
4024 浏览
提问于 2025-04-18 09:23

我正在尝试关注一组用户,并跟踪与这些用户发的推文匹配的关键词。

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)

请帮我解决这个问题。

1 个回答

0

我最开始的想法是,流式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"])

撰写回答