Python重定向表单 Action

标签 python action mechanize url-redirection http-status-code-302

我正在使用 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/

相关文章:

python - Pandas 可以使用列作为索引吗?

Java只有在正确的情况下才能在其他框架中登录

ruby - 我在 ruby​​ 中安装 Mechanize 时的一些问题

python - Spark动态帧显示方法没有产生任何结果

python - 获取任何可调用对象的参数?

python - 字典键数

reactjs - React/Redux - 将变量传递给操作

button - jqgrid EditActionIconsColumn 事件

python - Mechanize + 异步浏览器调用

ruby - 如何让 Mechanize 自动将正文转换为 UTF8?