【发布时间】:2015-08-25 19:34:03
【问题描述】:
我正在使用以下代码(来自 django 管理命令)来收听 Twitter 流 - 我在单独的命令中使用了相同的代码来成功跟踪关键字 - 我已经将其扩展到使用位置,并且(显然是正确的)想在不破坏我正在运行的现有分析的情况下对此进行测试。
我已关注文档并确保该框为 Long/Lat 格式(事实上,我现在使用的是 Twitter 文档中的示例 long/lat)。它看起来大致相同as the question here,我尝试使用答案中的代码版本 - 同样的错误。如果我切换回使用'track = ...',相同的代码可以工作,所以这是位置过滤器的问题。
在 tweepy 中的 streaming.py 中添加打印调试,以便我可以看到发生了什么,我从 _run 打印出 self.parameters self.url 和 self.headers,然后得到:
{'track': 't,w,i,t,t,e,r', 'delimited': 'length', 'locations': '-121.7500,36.8000,-122.7500,37.8000'}
/1.1/statuses/filter.json?delimited=length 和
{'Content-type': 'application/x-www-form-urlencoded'}
分别-在我看来,以某种形式或形式缺少对位置的搜索。我不相信我/我显然不是唯一一个使用tweepy location search 的人,所以认为这更可能是我使用它的问题而不是 tweepy 中的错误(我在 2.3.0 上),但是我的实现看起来很正确。
我的流处理代码在这里:
consumer_key = 'stuff'
consumer_secret = 'stuff'
access_token='stuff'
access_token_secret_var='stuff'
import tweepy
import json
# This is the listener, resposible for receiving data
class StdOutListener(tweepy.StreamListener):
def on_data(self, data):
# Twitter returns data in JSON format - we need to decode it first
decoded = json.loads(data)
#print type(decoded), decoded
# Also, we convert UTF-8 to ASCII ignoring all bad characters sent by users
try:
user, created = read_user(decoded)
print "DEBUG USER", user, created
if decoded['lang'] == 'en':
tweet, created = read_tweet(decoded, user)
print "DEBUG TWEET", tweet, created
else:
pass
except KeyError,e:
print "Error on Key", e
pass
except DataError, e:
print "DataError", e
pass
#print user, created
print ''
return True
def on_error(self, status):
print status
l = StdOutListener()
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret_var)
stream = tweepy.Stream(auth, l)
#locations must be long, lat
stream.filter(locations=[-121.75,36.8,-122.75,37.8], track='twitter')
【问题讨论】:
标签: python-2.7 twitter geolocation tweepy