我想用python点击一个按钮,表单的信息由网页自动填充。向按钮发送请求的 HTML 代码是:
INPUT type="submit" value="Place a Bid">
我该怎么做呢?是否可以仅使用 urllib 或 urllib2 单击按钮?或者我需要使用机械化或斜纹布之类的东西吗?
使用表单目标并将任何输入作为发布数据发送,如下所示:
<form target="http://mysite.com/blah.php" method="GET">
......
......
......
<input type="text" name="in1" value="abc">
<INPUT type="submit" value="Place a Bid">
</form>
Python:
# parse the page HTML with the form to get the form target and any input names and values... (except for a submit and reset button)
# You can use XML.dom.minidom or htmlparser
# form_target gets parsed into "http://mysite.com/blah.php"
# input1_name gets parsed into "in1"
# input1_value gets parsed into "abc"
form_url = form_target + "?" + input1_name + "=" + input1_value
# form_url value is "http://mysite.com/blah.php?in1=abc"
# Then open the new URL which is the same as clicking the submit button
s = urllib2.urlopen(form_url)
您可以使用HTMLParser解析 HTML
并且不要忘记使用以下命令对任何帖子数据进行 urlencode:
您可能想查看 IronWatin - https://github.com/rtyler/IronWatin以填写表格并使用代码“单击”按钮。
使用 urllib.urlopen,您可以将表单的值作为数据参数发送到表单标记中指定的页面。但这不会为您自动化浏览器,因此您必须首先以其他方式获取表单值。