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
qooweds
V2EX  ›  Python

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

  •  
  •   qooweds · 2016-04-27 11:15:37 +08:00 · 7155 次点击
    这是一个创建于 2934 天前的主题,其中的信息可能已经有所发展或是发生改变。
    用 fiddler 抓包看到 302 的 URL 字段貌似是 Location ,可是我把 Header 信息打印出来没有这个字段的信息。
    我是这样获取的:
    r = requests.head(url)
    print r.headers

    请问 requests 能实现这个需求吗?或者说需要用到 httplib 才行呢?
    4 条回复    2016-04-27 11:43:40 +08:00
    caspartse
        2
    caspartse  
       2016-04-27 11:34:49 +08:00   ❤️ 1
    r.history
    lxy
        3
    lxy  
       2016-04-27 11:39:38 +08:00   ❤️ 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
       2016-04-27 11:43:40 +08:00
    感谢两位!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3554 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:20 · PVG 19:20 · LAX 04:20 · JFK 07:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.