V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
alvin666
V2EX  ›  Python

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

  •  
  •   alvin666 · 2018-08-18 14:52:50 +08:00 · 6710 次点击
    这是一个创建于 2069 天前的主题,其中的信息可能已经有所发展或是发生改变。

    抓包安卓 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 中的某些项,文档中没找见....

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

    请参考文档
    alvin666
        4
    alvin666  
    OP
       2018-08-18 15:09:01 +08:00 via Android
    @kmahyyg 我就是这么做的,但是只能覆盖,如果值空白,发送的请求里面也有,不能删除已有的 cookie,文档里面没有提到我才来问的,谢谢
    stamaimer
        5
    stamaimer  
       2018-08-18 15:14:19 +08:00 via iPhone
    None
    alvin666
        6
    alvin666  
    OP
       2018-08-18 15:14:55 +08:00 via Android
    @stamaimer 详细说说?....意思是没办法?
    holajamc
        7
    holajamc  
       2018-08-18 15:21:29 +08:00   ❤️ 2
    @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
        8
    Xiaobaixiao  
       2018-08-18 15:29:12 +08:00   ❤️ 2
    alvin666
        9
    alvin666  
    OP
       2018-08-18 16:56:51 +08:00 via Android
    @Xiaobaixiao @holajamc 谢谢!最近被墙的厉害,没仔细在谷歌上面找,感谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3661 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 05:00 · PVG 13:00 · LAX 22:00 · JFK 01:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.