使用twython获取Geolocation的推文

时间:2014-12-19 13:34:33

标签: python twitter twython

我试图按地理位置搜索推文。我试图在纽约发送推文:

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.

2 个答案:

答案 0 :(得分:0)

地理编码参数应与最终的Twitter搜索调用中的以下内容类似:

41.8734,-70.6394,5mi

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

  

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

答案 1 :(得分:0)

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)