我们如何从特定国家获得推文
我已经阅读了很多有关此部分的内容,发现是编写地理编码并搜索tweet,例如
https://api.twitter.com/1.1/search/tweets.json?geocode=37.781157,-122.398720,1mi&count
=
10
根据我在Twitter网站上发现的信息,返回位于给定纬度/经度半径内的用户的推文。使用“半径”修改器时,最多将考虑1,000个不同的“子区域”。值范例:37.781157,-122.398720,1mi
问题!,我们如何定义或绘制纬度和经度?我已经尝试过谷歌地图,但我只得到一个点,然后我就可以添加该点周围的里程,但这还不够,我希望将整个国家都包括在内,这可能吗?
-
一种方法是使用twitter地理搜索API,获取地点ID,然后使用进行常规搜索
place:place_id
。例如,使用tweepy:import tweepy auth = tweepy.OAuthHandler(..., ...) auth.set_access_token(..., ...) api = tweepy.API(auth) places = api.geo_search(query="USA", granularity="country") place_id = places[0].id tweets = api.search(q="place:%s" % place_id) for tweet in tweets: print tweet.text + " | " + tweet.place.name if tweet.place else "Undefined place"
UPD(使用python-twitter的相同示例):
from twitter import * t = Twitter(auth=OAuth(..., ..., ..., ...)) result = t.geo.search(query="USA", granularity="country") place_id = result['result']['places'][0]['id'] result = t.search.tweets(q="place:%s" % place_id) for tweet in result['statuses']: print tweet['text'] + " | " + tweet['place']['name'] if tweet['place'] else "Undefined place"
希望能有所帮助。