【问题标题】:Tweepy location on Twitter API filter always throws 406 errorTwitter API 过滤器上的 Tweepy 位置总是抛出 406 错误
【发布时间】: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.urlself.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


    【解决方案1】:

    这里的问题是坐标的顺序。

    正确的格式是: 西南角(Long, Lat),东北角(Long, Lat)。我把它们调换了。 :(

    【讨论】:

      【解决方案2】:

      流式传输 API 不允许同时按位置和关键字进行过滤。 你必须参考这个答案 我之前有同样的问题 https://stackoverflow.com/a/22889470/4432830

      【讨论】:

      • 问题不是/不是返回的推文没有按预期返回,而是它没有返回任何东西。不过,坐标的格式在那个答案中是正确的,但不是我提供的那些,我不知道为什么。 :(
      猜你喜欢
      • 2017-09-13
      • 2019-07-11
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 2017-08-02
      • 2017-07-31
      • 2021-02-23
      相关资源
      最近更新 更多