我想用python点击一个按钮,表单的信息由网页自动填写。向按钮发送请求的HTML代码为:
INPUT type="submit" value="Place a Bid">我该怎么做呢?是否可以仅使用urllib或urllib2来单击该按钮?或者我需要使用像机械化或斜纹之类的东西?
发布于 2011-10-03 11:42:48
使用表单目标并将任何输入作为post数据发送,如下所示:
<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对任何post数据进行编码:
urllib.urlencode(query)
发布于 2011-10-03 11:42:21
您可能希望查看IronWatin - https://github.com/rtyler/IronWatin来填充表单,然后使用代码“单击”按钮。
发布于 2011-10-03 11:34:56
使用urllib.urlopen,您可以将表单的值作为data参数发送到form标记中指定的页面。但这不会自动为您的浏览器,所以您必须先以其他方式获取表单值。
https://stackoverflow.com/questions/7630795
复制相似问题