我想点击一个带有
python的按钮,该表单的信息将自动填满网页.用于向按钮发送请求的HTML代码是:
INPUT type="submit" value="Place a Bid">
我该怎么做呢?
是否可以用urllib或urllib2单击按钮?还是需要使用机械化或斜纹呢?
解决方法
使用表单目标并发送任何输入作为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任何发布数据: