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

请问网页 Python requests.post 后 跳转的地址是?

  •  
  •   hititan · Jul 6, 2017 · 4075 views
    This topic created in 3259 days ago, the information mentioned may be changed or developed.

    小弟不才,这个小问题想了和搜索了,还是没有眉目,请你们指点一下。 正常使用浏览器步骤如下: A.html 页面,我填写好价格参数,python requests.post 后会跳转到 B.html 页面,需要在 B.html 里填写帐号密码再 post 上去。请问怎样从 post A.html 后知道将要跳转哪个页面?也就是说在 respond 返回 A 页面里能找到跳转的页面么?如果您没有看明白,那一定是我表达不清晰,非常抱歉,请指点一下。

    3 replies    2017-07-07 09:36:59 +08:00
    ri0day
        1
    ri0day  
       Jul 6, 2017   ❤️ 1
    如果是返回 302,response header 里面就有 location 这个 header
    r = requests.post('http://xyz.com/302',data = data)
    r.headers['Location']

    还有一个简单做法就是:
    r = requests.post(url, allow_redirects=True)
    print(r.url)
    hititan
        2
    hititan  
    OP
       Jul 6, 2017
    @ri0day 感谢你的回复
    请问你这个简单的做法,是在 post A 页面(比如说登录 A 页面成功)的情况下,用 r = requests.post(url, allow_redirects=True)
    print(r.url)
    就返回新的跳转地址对吗?
    我马上测试,但还是想这样确定一下:)
    ri0day
        3
    ri0day  
       Jul 7, 2017
    是的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3024 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:22 · PVG 22:22 · LAX 07:22 · JFK 10:22
    ♥ Do have faith in what you're doing.