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

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

  •  
  •   hititan · 2017-07-06 16:31:26 +08:00 · 3017 次点击
    这是一个创建于 2458 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    3 条回复    2017-07-07 09:36:59 +08:00
    ri0day
        1
    ri0day  
       2017-07-06 16:41:28 +08:00   ❤️ 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
       2017-07-06 16:52:14 +08:00
    @ri0day 感谢你的回复
    请问你这个简单的做法,是在 post A 页面(比如说登录 A 页面成功)的情况下,用 r = requests.post(url, allow_redirects=True)
    print(r.url)
    就返回新的跳转地址对吗?
    我马上测试,但还是想这样确定一下:)
    ri0day
        3
    ri0day  
       2017-07-07 09:36:59 +08:00
    是的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2822 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 14:34 · PVG 22:34 · LAX 07:34 · JFK 10:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.