[英]Submitting form with python
我需要在python脚本中解析HTML。 但是,要进入该页面,我需要在浏览器中单击单选按钮并提交表单。 那么,如何使用python提交表单并返回html? 谢谢,我将不胜感激。
我正在谈论的形式:
<form id="ac" method="post">
<input type="radio" id="a" name="a" value="2" onchange="document.getElementById('ac').submit();" checked="checked">
<input type="radio" id="aa" name="a" value="1" onchange="document.getElementById('ac').submit();">
<input type="radio" id="aaa" name="a" value="0" onchange="document.getElementById('ac').submit();">
</form>
您可以为此使用urllib: http : //docs.python.org/2.7/library/urllib.html
单选按钮将其数据发送为name = value,在这种情况下为a = 2。
来自Python URLLib / URLLib2 POST的修改示例:
import urllib
import httplib
server='myserver.com'
get_data='/link/with/get/data.php?test=1'
data = urllib.urlencode({'a': 2})
h = httplib.HTTPConnection('enfenion.com')
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
h.request('POST', get_data, data, headers)
r = h.getresponse()
print r.read()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.