推荐学习书目
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
qooweds
V2EX  ›  Python

requests 模块能获取 Response Headers 里面 302 的 URL 吗?

  •  
  •   qooweds · Apr 27, 2016 · 7758 views
    This topic created in 3742 days ago, the information mentioned may be changed or developed.
    用 fiddler 抓包看到 302 的 URL 字段貌似是 Location ,可是我把 Header 信息打印出来没有这个字段的信息。
    我是这样获取的:
    r = requests.head(url)
    print r.headers

    请问 requests 能实现这个需求吗?或者说需要用到 httplib 才行呢?
    4 replies    2016-04-27 11:43:40 +08:00
    caspartse
        2
    caspartse  
       Apr 27, 2016   ❤️ 1
    r.history
    lxy
        3
    lxy  
       Apr 27, 2016   ❤️ 2
    requests 默认自动处理跳转,不想这样的话就设置 allow_redirects = False 。
    >>> res = requests.get('http://bing.com', allow_redirects=False)
    >>> res.headers['location']
    'http://cn.bing.com/'
    qooweds
        4
    qooweds  
    OP
       Apr 27, 2016
    感谢两位!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2842 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 00:21 · PVG 08:21 · LAX 17:21 · JFK 20:21
    ♥ Do have faith in what you're doing.