我正在尝试对本地内部门户网站执行GET请求调用,以从门户网站中获取数据。我可以使用以下代码成功完成此操作。但是我很难在请求标头中编码“ cookie”参数,将来可能会更改。如果我没有在标题中给cookie,我会收到403响应。无论如何,没有硬编码“ cookie”头就可以进行GET调用吗?
PS-我是以下代码中chrome开发人员工具的“请求标头”中提到的标头。chrome开发人员工具中的请求标头:
GET /api/router/service/instance-33/api/monitoring/metrics/summary?timeline=10m&metricGroupType=Client HTTP/1.1
Host: xxx
Connection: keep-alive
Accept: application/json
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36
Content-Type: application/json
Referer: xxx
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: _ga=GA1.2.1166979480.1557148769; _rollup=GA1.2.1925782236.1557148769; _gid=GA1.2.572392410.1560761227; VIDUSR=1560762756-eXdFaeT29yEDoA%3d%3d
代码即时通讯使用:
import requests
url = 'xxx'
proxies = {
'xxx/',
'xxx',
}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36', 'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.9', 'Connection': 'keep-alive', 'Content-Type': 'application/json', 'Cookie': '_ga=GA1.2.1166979480.1557148769; _rollup=GA1.2.1925782236.1557148769; _gid=GA1.2.572392410.1560761227; VIDUSR=1560762756-eXdFaeT29yEDoA%3d%3d', 'Host': 'xxx', 'Referer': 'xxx', 'X-Requested-With': 'XMLHttpRequest'}
r = requests.get(url, proxies=proxies, verify=False, headers=headers)
print(r.json())
print(r)