当使用浏览器或请求访问页面时,为什么来自nomatim的响应不同?

时间:2015-07-17 13:35:40

标签: python json python-requests openstreetmap nominatim

我从此网址获取JSON数据

http://nominatim.openstreetmap.org/?q=san%20francisco&format=json&addressdetails=1&limit=1

在网络浏览器上查看时输出

[{"display_name":"San Francisco City and County, California, United States of America","class":"boundary","type":"administrative","importance":0.88836369596997,"icon":"http:\/\/nominatim.openstreetmap.org\/images\/mapicons\/poi_boundary_administrative.p.20.png","address":{"county":"San Francisco City and County","state":"California","country":"United States of America","country_code":"us"}}]

当我从一个简单的python程序中获取相同的URL时

import requests

r = requests.get('http://nominatim.openstreetmap.org/?q=san%20francisco&format=json&addressdetails=1&limit=1')
json_data = r.json()

print json_data

这就是我得到的,

[{u'display_name': u'SF, California, United States of America', u'importance': 0.68836369596997, u'place_id': u'127766562', u'lon': u'-122.4629897', u'lat': u'37.7647993', u'osm_type': u'relation', u'licence': u'Data \xa9 OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright', u'osm_id': u'396487', u'boundingbox': [u'37.63983', u'37.9298443', u'-123.1738249', u'-122.2817799'], u'type': u'administrative', u'class': u'boundary', u'address': {u'county': u'SF', u'country': u'United States of America', u'state': u'California', u'country_code': u'us'}, u'icon': u'http://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png'}]

请注意,display_name以SF,California, Unite.....

开头

我尝试删除所有历史记录/ Cookie /缓存甚至重新启动计算机

发生了什么事?

1 个答案:

答案 0 :(得分:5)

服务器查找Accept-Language标头;不发送一个,你得到缩写,发一个说你接受英语,你得到全名:

>>> import requests
>>> url = 'http://nominatim.openstreetmap.org/'
>>> params = {'q': 'san francisco', 'format': 'json', 'addressdetails': 1, 'limit': 1}
>>> requests.get(url, params=params).json()[0]['display_name']
u'SF, California, United States of America'
>>> requests.get(url, params=params, headers={'Accept-Language': 'en'}).json()[0]['display_name']
u'San Francisco City and County, California, United States of America'

Nomatim documentation确实提到使用了标头,但可以通过在查询字符串中指定accept-language参数来覆盖:

>>> print requests.get(url, params=dict(params, **{'accept-language': 'pt'})).json()[0]['display_name']
SF, Califórnia, Estados Unidos da América