gpt4 book ai didi

Python重定向表单 Action

转载 作者:行者123 更新时间:2023-12-04 16:21:14 25 4
gpt4 key购买 nike

我正在使用 mechanize 和 python 2.7 向带有表单的页面发送请求,这次是获取数据

<form method='get' action='/470711262846118652822405241081316835027/new2'>
<textarea name='name' rows='5' style='width:100%'></textarea>
<br>
<table summary='' style='width:100%'>
<tr>
<td align='right' valign='top'>
<input type='submit' value='Submit'>
</td>
</tr>
</table>
</form>

当我
try:
request = Request(url,data,header)
response = urlopen(request)
html = response.read()
return html
except IOError, e:
print "*Cant open {0}{1}{2}".format(fg(2), url,attr(0))
print "*Reason:{0}".format(e.reason)
return None

我在请求中使用的 url 是 www.foo.com/new?name=bar 但 Action 是 new2 并且这个 Action 将我重定向到另一个页面,我无法处理这个。
当我在浏览器中手动执行所有这些操作时,我可以使用检查元素从重定向中看到 302 代码。

标题、cookies、数据和一切都很好,因为我得到了 200 https 代码,我可以读取页面,但不能重定向

最佳答案

我不太懂 Mechanize ,不过你好像还没有设置set_handle_redirect ,所以 302 状态码没有被处理。
http://mechanize.readthedocs.io/en/latest/browser_api.html?highlight=handle#mechanize.Browser.set_handle_redirect

如果你没有设置这个,你必须手动检查 HTTP 状态 302 并阅读 location header 以查找重定向目标 URL。

关于Python重定向表单 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45845742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com