我正试图从包含费德勒的旧金山取消所有推文。我使用locations关键字收到错误。
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import csv
access_token = ''
access_secret = ''
consumer_key = ''
consumer_secret = ''
csvfile = open('StreamSearch.csv','a')
csvwriter = csv.writer(csvfile, delimiter = ',')
class StdOutListener(StreamListener):
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
if __name__ == '__main__':
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
stream = Stream(auth, l)
stream.filter(track='federer',locations = '-122.75,36.8,-121.75,37.8')
#stream.filter(track='djokovic')
错误是
追踪(最近一次呼叫最后一次):
File "C:\EclipseWorkspaces\csse120\9sept\src\SearchTweets.py", line 35, in <module>
stream.filter(track='federer',locations = '-122.75,36.8,-121.75,37.8')
File "C:\Python27\lib\site-packages\tweepy\streaming.py", line 307, in filter
raise TweepError("Wrong number of locations points, "
tweepy.error.TweepError: Wrong number of locations points, it has to be a multiple of 4
答案 0 :(得分:3)
你的代码在stream.Filter阶段之前是好的。过滤查询被Tweepy视为列表而非单个字符串。
所以如果你想用费德勒找到所有东西,你会搜索
stream.Filter ( track = ['federer'])
对于您将使用的位置
stream.Filter ( locations = [-122.75,36.8,-121.75,37.8] )
请注意,地点不是字符串,也不需要引号。此外,Twitter将JSON格式用于Longitude,Latitude的位置,这与Google Maps和其他服务提供的典型Latitude,Longtitude坐标不同。
不幸的是,您将无法跟踪包含旧金山费德勒的推文,因为当您拥有位置和跟踪过滤器时,它将成为跟踪或位置查询。这意味着您将获得包含费德勒或来自旧金山的推文。我不知道您可以对各种过滤器进行AND查询的方式。我不相信Twitter支持他们。您可以选择从San Fran收集推文,然后查看是否提及费德勒或费德勒的推文,然后按位置过滤。你仍然可以连接位置和轨道,但它将是这两个的OR查询,看起来像这样:
stream.Filter ( track = ['Federer'], locations = [-122.75,36.8,-121.75,37.8] )
有关Tweepy和Twitter参数的文档,请访问: https://dev.twitter.com/streaming/overview/request-parameters http://tweepy.readthedocs.org/en/v2.3.0/api.html?highlight=location
希望这会有所帮助。