不使用提交按钮提交,机械化

Aev*_*eus 10 python mechanize

所以,我从使用Mechanize开始,显然我尝试的第一件事是猴子 - 犀牛级高JavaScript导航网站.

现在,我坚持的是提交表格.

通常我会使用Mechanize内置的submit()函数进行提交.

import mechanize

browser = mechanize.Browser()
browser.select_form(name = 'foo')
browser.form['bar'] = 'baz'
browser.submit()
Run Code Online (Sandbox Code Playgroud)

这样它就可以使用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();
}
Run Code Online (Sandbox Code Playgroud)

我在Python中做了什么(以及什么不起作用):

def foo(browser, bar, baz):
    qux = browser.select_form("qux")

    browser.form[bar] = ":".join(bar.split("$"))
    browser.form[baz] = baz
    browser.submit()
Run Code Online (Sandbox Code Playgroud)

Pad*_*Ray 11

三种方式:

如果使用POST/GET方法提交表单,则第一种方法更可取,否则您将不得不采用第二种和第三种方法.

  1. 手动提交表单并检查POST/GET请求,其参数以及提交表单所需的帖子URL.用于检查标头的常用工具是Firefox的Live HTTP标头扩展和Firebug扩展,以及Chrome的Developer Tools扩展.使用POST/GET方法的示例:

    import mechanize
    import urllib
    
    browser = mechanize.Browser()
    #These are the parameters you've got from checking with the aforementioned tools
    parameters = {'parameter1' : 'your content',
                  'parameter2' : 'a constant value',
                  'parameter3' : 'unique characters you might need to extract from the page'
                 }
    #Encode the parameters
    data = urllib.urlencode(parameters)
    #Submit the form (POST request). You get the post_url and the request type(POST/GET) the same way with the parameters.
    browser.open(post_url,data)
    #Submit the form (GET request)
    browser.open(post_url + '%s' % data)
    
    Run Code Online (Sandbox Code Playgroud)
  2. 重写javascript并在Python中执行它.看看spidermonkey.

  3. 模拟完整的浏览器.看看Selenium和Windmill.