使用Twitter API和Python(地理编码)
我该如何在Python中实现以下调用呢?这是伪代码版本:
jsonTwitterResponse = twitter.get(up to max of 3
tweets within 3km of longitude: 7, latitude: 5)
print jsonTwitterResponse
看起来我需要的是geocode这个API。不过我不知道怎么把它真正写出来。那我该如何用实际的代码来实现上面的内容呢?
3 个回答
0
我觉得这个方法可能是最近才添加的:
import urllib, json, pprint
params = urllib.urlencode(dict(lat=37.76893497, long=-122.42284884))
u = urllib.urlopen('https://api.twitter.com/1/geo/reverse_geocode.json?' + params)
j = json.load(u)
pprint.pprint(j)
文档链接: https://dev.twitter.com/docs/api/1/get/geo/reverse_geocode
3
除了Raymond Hettinger的回答,我还想提一下,如果你不想使用具体的坐标,也可以用类似 "near:Amsterdam within:5km"
这样的查询方式。
举个例子:你可以访问这个链接 http://search.twitter.com/search?q=near:Amsterdam%20within:5km
来查看相关内容。4
这里有一个示例的 地理编码 请求:
import urllib, json, pprint
params = urllib.urlencode(dict(q='obama', rpp=10, geocode='37.781157,-122.398720,1mi'))
u = urllib.urlopen('http://search.twitter.com/search.json?' + params)
j = json.load(u)
pprint.pprint(j)
完整的 Twitter REST API 在这里有详细介绍: https://dev.twitter.com/docs/api。另外,Twitter 还有一个关于 地点搜索的常见问题解答,可能会对你有帮助。