不使用提交按钮提交,Mechanize
所以,我开始使用 Mechanize,显然我尝试的第一件事是一个猴子犀牛级别的高 JavaScript 导航网站。
现在我坚持的事情是提交表格。
通常我会使用 Mechanize 内置的 Submit() 函数进行提交。
import mechanize
browser = mechanize.Browser()
browser.select_form(name = 'foo')
browser.form['bar'] = 'baz'
browser.submit()
这样,它将使用 HTML 表单中提供的提交按钮。
然而,我所停留的网站必须是一个不使用 HTML 提交按钮的网站...不,他们试图成为 JavaScript 专家,并通过 JavaScript 进行提交。
通常的submit()似乎不适用于此。
那么...有办法解决这个问题吗?
任何帮助表示赞赏。非常感谢!
--[编辑]--
我所坚持的 JavaScript 函数:
function foo(bar, baz) {
var qux = document.forms["qux"];
qux.bar.value = bar.split("$").join(":");
qux.baz.value = baz;
qux.submit();
}
我在 Python 中做了什么(以及什么不起作用):
def foo(browser, bar, baz):
qux = browser.select_form("qux")
browser.form[bar] = ":".join(bar.split("$"))
browser.form[baz] = baz
browser.submit()
So, I started out with Mechanize, and apparently the first thing I try it on is a monkey-rhino-level high JavaScript navigated site.
Now the thing I'm stuck on is submitting the form.
Normally I'd do a submit using the Mechanize built-in submit() function.
import mechanize
browser = mechanize.Browser()
browser.select_form(name = 'foo')
browser.form['bar'] = 'baz'
browser.submit()
This way it'd use the submit button that's available in the HTML form.
However, the site I'm stuck on had to be one that doesn't use HTML submit buttons... No, they're trying to be JavaScript gurus, and do a submit via JavaScript.
The usual submit() doesn't seem to work with this.
So... Is there a way to get around this?
Any help is appreciated. Many thanks!
--[Edit]--
The JavaScript function I'm stuck on:
function foo(bar, baz) {
var qux = document.forms["qux"];
qux.bar.value = bar.split("$").join(":");
qux.baz.value = baz;
qux.submit();
}
What I did in Python (and what doesn't work):
def foo(browser, bar, baz):
qux = browser.select_form("qux")
browser.form[bar] = ":".join(bar.split("$"))
browser.form[baz] = baz
browser.submit()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
三种方法:
如果使用 POST/GET 方法提交表单,则首选第一种方法,否则您将不得不求助于第二种和第三种方法。
手动提交表单并检查 POST/GET 请求、其参数以及提交表单所需的发布 URL。用于检查标头的流行工具是 Firefox 的 Live HTTP 标头扩展和 Firebug 扩展以及 Chrome 的开发人员工具扩展。使用 POST/GET 方法的示例:
重写 javascript 并在 Python 中执行。查看 Spidermonkey。
模拟完整的浏览器。查看 Selenium 和 Windmill。
Three ways:
The first method is preferable if the form is submitted using the POST/GET method, otherwise you'll have to resort to second and third method.
Submitting the form manually and check for POST/GET requests, their parameters and the post url required to submit the form. Popular tools for checking headers are the Live HTTP headers extension and Firebug extension for Firefox, and Developer Tools extension for Chrome. An example of using the POST/GET method:
Rewrite the javascript and execute it in Python. Check out spidermonkey.
Emulate a full browser. Check out Selenium and Windmill.