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

2017-07-06 16:31:26 +08:00
 hititan

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

3042 次点击
所在节点    Python
3 条回复
ri0day
2017-07-06 16:41:28 +08:00
如果是返回 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
2017-07-06 16:52:14 +08:00
@ri0day 感谢你的回复
请问你这个简单的做法,是在 post A 页面(比如说登录 A 页面成功)的情况下,用 r = requests.post(url, allow_redirects=True)
print(r.url)
就返回新的跳转地址对吗?
我马上测试,但还是想这样确定一下:)
ri0day
2017-07-07 09:36:59 +08:00
是的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/373516

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX