求助如何删除 requests 中的某些 header

2018-08-18 14:52:50 +08:00
 alvin666

抓包安卓 app,发现实际请求的 header 是

Content-Type: application/x-www-form-urlencoded; charset=UTF-8
token: sasasasasasasasasasas
Host: ********:8443
Connection: Keep-Alive

用 requests 模拟,添加上述 header 以后,发现实际发送的请求经过 requests 处理,header 增加了几项(带*的)

*User-Agent: python-requests/2.19.1
*Accept-Encoding: gzip, deflate
*Accept: */*
Connection: Keep-Alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Host: *******:8443
*Content-Length: 0
token: sasasasasasasasasasas

求助怎么完全自定义 requests 的 header,或者删除 header 中的某些项,文档中没找见....

6723 次点击
所在节点    Python
9 条回复
julyclyde
2018-08-18 14:59:19 +08:00
不能删除
你可以覆盖掉它
alvin666
2018-08-18 15:02:23 +08:00
@julyclyde 是可以覆盖,比如模拟浏览器操作的时候,header 换成 Chrome,然而这个是 app 的请求,万一服务端来个 header 过滤就 gg...
kmahyyg
2018-08-18 15:06:10 +08:00
```python
custom_headers={"user-agent": "", ......}
r = requests.get(url, headers=custom_headers}
```

请参考文档
alvin666
2018-08-18 15:09:01 +08:00
@kmahyyg 我就是这么做的,但是只能覆盖,如果值空白,发送的请求里面也有,不能删除已有的 cookie,文档里面没有提到我才来问的,谢谢
stamaimer
2018-08-18 15:14:19 +08:00
None
alvin666
2018-08-18 15:14:55 +08:00
@stamaimer 详细说说?....意思是没办法?
holajamc
2018-08-18 15:21:29 +08:00
@alvin666
@stamaimer 的意思是这样
import requests

proxies = {
"http": "http://127.0.0.1:8888",
"https": "http://127.0.0.1:8888",
}
headers = {'User-Agent': None}
requests.get("http://example.org", headers=headers, proxies=proxies)
Xiaobaixiao
2018-08-18 15:29:12 +08:00
alvin666
2018-08-18 16:56:51 +08:00
@Xiaobaixiao @holajamc 谢谢!最近被墙的厉害,没仔细在谷歌上面找,感谢!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/480977

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX