我们如何从特定国家获得推文 [英] How can we get tweets from specific country

查看:121
本文介绍了我们如何从特定国家获得推文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多有关此部分的内容,发现是编写地理编码并搜索推文 例如 https://api.twitter. com/1.1/search/tweets.json?geocode = 37.781157,-122.398720,1mi& count = 10

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

根据我在Twitter网站上发现的内容 返回位于给定纬度/经度半径内的用户的推文. 使用半径"修改器时,最多将考虑1,000个不同的子区域". 示例值:37.781157,-122.398720,1mi

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

问题!,我们如何定义或绘制纬度和经度?我已经尝试过Google Map,但是我只得到一个点,然后我可以添加该点周围的里程,但这还不够,我希望将整个国家都包括在内,这有可能吗?

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?

推荐答案

一种方法是使用Twitter 地理搜索API ,获取地点ID,然后使用place:place_id进行常规搜索.例如,使用 tweepy :

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:

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的相同示例):

UPD (the same example using 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"

希望有帮助.

这篇关于我们如何从特定国家获得推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆