使用流式API通过多个用户进行多项搜索
我正在尝试通过关注特定用户来获取包含多个关键词的推文。下面是我使用的代码:
之前我发过一段代码,讨论了关于值错误的问题:我想办法解决了那个问题,但现在又遇到了这个错误追踪。
import tweepy
from tweepy.error import TweepError
consumer_key=('ABC'),
consumer_secret=('ABC'),
access_key=('ABC'),
access_secret=('ABC')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api=tweepy.API(auth)
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
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
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 obey:
self.parameters['follow'] = ' '.join(map(str, obey))
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
streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)
list_users = ['17006157','59145948','157009365','16686144','68044757','33338729']#Some ids
list_terms = ['narendra modi','robotics']#Some terms
streaming_api.filter(follow=[list_users])
streaming_api.filter(track=[list_terms])
我收到了一个错误追踪:
Traceback (most recent call last):
File "C:\Python27\nytimes\26052014\Multiple term search with multiple addreses.py", line 49, in <module>
streaming_api.filter(follow=[list_users])
File "build\bdist.win32\egg\tweepy\streaming.py", line 296, in filter
encoded_follow = [s.encode(encoding) for s in follow]
AttributeError: 'list' object has no attribute 'encode'
请帮我解决这个问题。
1 个回答
3
你在这里定义了 list_users
list_users = ['17006157','59145948','157009365','16686144','68044757','33338729']
然后你把它传给 streaming_api.filter
,像这样
streaming_api.filter(follow=[list_users])
当 streaming_api.filter
函数在处理你传入的 follow
值时,它出现了一个错误
AttributeError: 'list' object has no attribute 'encode'
这个错误的原因如下
你是这样调用 streaming_api.filter
的
streaming_api.filter(follow=[list_users])
在这里
streaming_api.filter(follow=[list_users])
你试图把你的列表作为 follow
的值传入,但因为你把 list_users
放在了外面的 []
中,所以你实际上是创建了一个列表中的列表。然后 streaming_api.filter
在遍历 follow
时,会对每个条目调用 .encode
,就像我们在这里看到的
[s.encode(encoding) for s in follow]
但是这个条目 s
是一个列表,而它应该是一个字符串。这是因为你不小心创建了一个列表中的列表,正如上面所示。
解决这个问题很简单。把
streaming_api.filter(follow=[list_users])
改成
streaming_api.filter(follow=list_users)
要把一个列表传给函数,你只需要指定它的名字。没有必要把它放在 []
中
最后一行也是一样。把
streaming_api.filter(track=[list_terms])
改成
streaming_api.filter(track=list_terms)