Tweepy location on Twitter API 过滤器总是抛出 406 错误
Tweepy location on Twitter API filter always throws 406 error
我正在使用以下代码(来自 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'}
分别 - 在我看来似乎错过了以某种方式或形式搜索位置。我不相信我'm/I显然不是唯一使用 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')
流媒体 API 不允许同时按位置和关键字进行过滤。
你必须参考这个答案我之前有同样的问题
这里的问题是坐标的顺序。
正确的格式是:
西南角(经度,纬度),东北角(经度,纬度)。我把它们调换了。 :(
我正在使用以下代码(来自 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'}
分别 - 在我看来似乎错过了以某种方式或形式搜索位置。我不相信我'm/I显然不是唯一使用 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')
流媒体 API 不允许同时按位置和关键字进行过滤。 你必须参考这个答案我之前有同样的问题
这里的问题是坐标的顺序。
正确的格式是: 西南角(经度,纬度),东北角(经度,纬度)。我把它们调换了。 :(