我已经阅读了很多关于这部分的内容,我发现编写地理编码和搜索推文 例如 https://api.twitter.com/1.1/search/tweets.json?geocode=37.781157,-122.398720,1mi&count=10
根据我在推特网站上找到的 返回位于给定纬度/经度的给定半径内的用户的推文。 使用半径修改器时,将考虑最多 1,000 个不同的“子区域”。 示例值:37.781157,-122.398720,1mi
问题!我们如何定义或绘制纬度和经度?我试过谷歌地图,但我只得到一个点,然后我可以添加这个点周围的英里数,但这还不够,我希望包括整个国家,这可能吗?
最佳答案
一种方法是使用推特 geo search 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"
另请参阅这些主题:
- iOS Twitter API; How to retrieve the most recent tweets within a country?
- How do I get top tweeps by country?
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"
希望对您有所帮助。
关于python - 我们如何获得来自特定国家/地区的推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17633378/