Python 请求超时 JSON

Python requests timing out on JSON

我住在圣地亚哥,我已经 运行 使用 SD 县 API 为我的社区提供了一个小的 covid 仪表板。这是 API 网站:https://sdgis-sandag.opendata.arcgis.com/datasets/covid-19-statistics-by-zip-code/api and here is the query I have been using: https://gis-public.sandiegocounty.gov/arcgis/rest/services/Hosted/COVID_19_Statistics__by_ZIP_Code/FeatureServer/0/query?where=ziptext%20%3D%20%2792131%27&outFields=*&outSR=4326&f=json

一切正常,直到该县实施了某种防火墙来防止 DDOS 攻击,我无法再访问此 API。我有一个简单的 python 请求,但它超时了:requests.post(srQuery,timeout=5) 与我交谈过的员工不知道为什么会发生这种情况,而且似乎无法解决它。

我能想到的唯一解决方法是使用浏览器访问 API,然后将文件保存到我的计算机,然后手动读取。有人有更好的主意吗?或者关于如何自动将浏览器文件保存到我的硬盘的想法?

您需要在请求中将 User-Agent 添加到您的 headers。

import requests

headers = {'User-Agent': '[insert user agent here]'}
r = requests.get("https://gis-public.sandiegocounty.gov/arcgis/rest/services/Hosted/COVID_19_Statistics__by_ZIP_Code/FeatureServer/0/query?where=ziptext%20%3D%20%2792131%27&outFields=*&outSR=4326&f=json", headers=headers)
print(r.status_code) # Should not time out. Should return with a status code of 200.