Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Google Places API请求在没有用户代理标头的情况下失败_Python_Python Requests_Google Places Api - Fatal编程技术网

Python Google Places API请求在没有用户代理标头的情况下失败

Python Google Places API请求在没有用户代理标头的情况下失败,python,python-requests,google-places-api,Python,Python Requests,Google Places Api,我们有一个python应用程序,它使用请求库与GooglePlaces进行大量对话。传统上,这看起来像这样: import requests url = "https://maps.googleapis.com/maps/api/place/details/json" payload = { "sensor": "false", "key": YOUR_KEY_HERE, "placeid": SOME_PLACE_ID_HERE, } response = requ

我们有一个python应用程序,它使用请求库与GooglePlaces进行大量对话。传统上,这看起来像这样:

import requests
url = "https://maps.googleapis.com/maps/api/place/details/json"
payload = {
    "sensor": "false",
     "key": YOUR_KEY_HERE,
     "placeid": SOME_PLACE_ID_HERE,
}
response = requests.get(url, params=payload)
content = response.text
然而,今天我们突然收到403个错误,说我们的流量不正常,导致整个应用程序宕机。经过多次尝试和错误,我尝试使用良好的ole urllib2库,它成功了,我的浏览器中的请求也完全一样

因此,我添加了一个完全没有意义的用户代理头,如下所示:

import requests
url = "https://maps.googleapis.com/maps/api/place/details/json"
payload = {
    "sensor": "false",
     "key": YOUR_KEY_HERE,
     "placeid": SOME_PLACE_ID_HERE,
}
response = requests.get(url, params=payload, headers={'user-agent': 'foo'})
content = response.text

这个问题消失了。有人知道这里发生了什么吗?

如果您没有在标题中定义用户代理,请求库将使用:“python请求/2.5.1 CPython/2.7.9 Windows/7”作为用户代理。我的猜测是谷歌触发了这一点。同样的问题在这里。。。只需将用户代理设置为“无”,它就可以工作了。。。说真的,谷歌???为什么拒绝用户代理??这是一个API-(在没有任何信息的情况下·······························。有什么消息吗?嗨,杰梅兹。如果未指定用户代理请求,是否可以确认正在发送的用户代理请求?如果您运行这个要点,它应该将用户代理打印到您的控制台:下面是脚本为我打印的内容(mac laptop最新os):UserAgent:python requests/2.7.0 CPython/2.7.6 Darwin/14.4.0