Python 我们如何从特定国家/地区获取推文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17633378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can we get tweets from specific country
提问by user2490790
I've read a lot about this part and what I found is to write the geocode and search for tweets for example https://api.twitter.com/1.1/search/tweets.json?geocode=37.781157,-122.398720,1mi&count=10
我已经阅读了很多关于这部分的内容,我发现是编写地理编码并搜索推文,例如 https://api.twitter.com/1.1/search/tweets.json?geocode=37.781157,-122.398720,1mi&count =10
according to what i found in twitter website Returns tweets by users located within a given radius of the given latitude/longitude. A maximum of 1,000 distinct "sub-regions" will be considered when using the radius modifier. Example Values: 37.781157,-122.398720,1mi
根据我在 twitter 网站上找到的内容返回位于给定纬度/经度的给定半径内的用户的推文。使用半径修改器时将考虑最多 1,000 个不同的“子区域”。示例值:37.781157,-122.398720,1mi
The question!, how can we define or draw the latitude and longitude ? I've tried google map but I only get a point then i can add the miles around this point, but this is not enough, I want the whole country to be included, is that possible?
问题!,我们如何定义或绘制纬度和经度?我试过谷歌地图,但我只得到一个点,然后我可以在这个点周围添加英里数,但这还不够,我想包括整个国家,这可能吗?
采纳答案by alecxe
One way is to use twitter geo search API, get the place id and then perform regular search using place:place_id
. Example, using tweepy:
一种方法是使用 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"
Also see these threads:
另请参阅这些主题:
- iOS Twitter API; How to retrieve the most recent tweets within a country?
- How do I get top tweeps by country?
UPD (the same example using python-twitter):
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"
Hope that helps.
希望有帮助。