我正在尝试通过我调用的网站检索响应 json 数据。
网站是这样的:
此页面上显示了 map 上的一些数据。使用浏览器调试器我可以看到终点
发送响应数据json。
我已经使用这个 python 来尝试抓取 json 响应数据:
import requests
import json
headers = {
'Host': 'api2.drive-now.com',
'X-Api-Key': 'adf51226795afbc4e7575ccc124face7'
}
r = requests.get('https://api2.drive-now.com/cities/42756?expand=full', headers=headers)
json_obj = json.loads(r.content)
但我收到此错误:
hostname doesn't match either of 'activityharvester.com'
我如何检索这些数据?
谢谢
我尝试使用 Postam 调用显示 json 响应的端点,并仅传递到 header 主机和 Api-Key。结果就是我想要的json。但我尝试对 python 进行相同的调用,但收到错误主机名与“activityharvester.com”都不匹配
最佳答案
我不明白你的脚本,也不明白你的问题。为什么有两个请求和三个 header ?您的意思是这样吗?
import requests
import json
headers = {
'User-Agent': 'Mozilla/5.0',
'X-Api-Key':'adf51226795afbc4e7575ccc124face7',
}
res = requests.get('https://api2.drive-now.com/cities/4604?expand=full', headers=headers, allow_redirects=False)
print(res.status_code, res.reason)
json_obj = json.loads(res.content)
print(json_obj)
关于Python网页抓取数据请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42482915/