【问题标题】:Get Tweets by Geolocation using twython使用 twython 按地理位置获取推文
【发布时间】:2015-05-13 11:50:24
【问题描述】:

我正在尝试按地理位置搜索推文。我正在尝试使用以下方式在纽约发布推文:

twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)

geocode = "42.3482° N, 75.1890° W"

search_results = twitter.search_geo(count=10,geocode=geocode)

   try:
       for tweet in search["statuses"]:
       print(tweet ['text'])

返回错误

Traceback (most recent call last):
  File "D:\Projects\M Tools\Twython\My works\new.py", line 18, in <module>
    search_results = twitter.search_geo(count=10,geocode=geocode)
  File "C:\Python34\lib\site-packages\twython-3.2.0-py3.4.egg\twython\endpoints.py", line 818, in search_geo
    return self.get('geo/search', params=params)
  File "C:\Python34\lib\site-packages\twython-3.2.0-py3.4.egg\twython\api.py", line 263, in get
    return self.request(endpoint, params=params, version=version)
  File "C:\Python34\lib\site-packages\twython-3.2.0-py3.4.egg\twython\api.py", line 257, in request
    api_call=url)
  File "C:\Python34\lib\site-packages\twython-3.2.0-py3.4.egg\twython\api.py", line 198, in _request
    retry_after=response.headers.get('X-Rate-Limit-Reset'))
twython.exceptions.TwythonError: Twitter API returned a 400 (Bad Request), You must provide valid coordinates, IP address, query, or attributes.

【问题讨论】:

    标签: python twitter twython


    【解决方案1】:

    在最终的 Twitter 搜索调用中,geocode 参数应类似于以下内容:

    41.8734,-70.6394,5mi
    

    根据 Twitter API 控制台中的调用 (https://dev.twitter.com/rest/tools/console)

    返回位于给定半径内的用户的推文 纬度/经度。该位置优先取自 地理标记 API,但将回退到他们的 Twitter 个人资料。这 参数值由“纬度,经度,半径”指定,其中 半径单位必须指定为“mi”(英里)或“km” (公里)。请注意,您不能通过 API 使用 near 运算符 对任意位置进行地理编码;但是您可以使用此地理编码 参数直接搜索附近的地理编码。最多 1,000 使用半径时将考虑不同的“子区域” 修饰符。

    【讨论】:

      【解决方案2】:

      Twython 中,search_geo 方法用于为提供的地理位置半径内的“地点”查找place_id。您需要像这样使用search 方法:

      from twython import Twython, TwythonError
      
      
      app_key = 'your_app_key'
      app_secret = 'your_app_secret'
      oauth_token = 'your_oauth_tokem'
      oauth_token_secret = 'your_oauth_secret'
      
      twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)
      
      geocode = '42.3482,75.1890,1mi' # latitude,longitude,distance(mi/km)
      
      search_results = twitter.search(count=10, geocode=geocode)
      
      try:
          for tweet in search_results['statuses']:
              print (tweet['text'])
      except TwythonError as e:
          print(e)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-14
        • 1970-01-01
        • 1970-01-01
        • 2021-01-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多