我正在尝试关注用户列表&跟踪列表中提到的任何用户推文匹配的任何推文的关键字列表。
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)
请帮我解决这个问题。
答案 0 :(得分:0)
我最初的想法是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=users
和track=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"])