用python提交表单 [英] Submitting form with python
问题描述
我需要在我的python脚本中解析HTML。然而,要在我的浏览器中访问我需要的页面,请单击单选按钮并提交表单。那么,我如何使用python提交表单并获取呈现的html?谢谢,我会很感激任何帮助。
我在谈论的形式:
< form id =acmethod =post>
< input type =radioid =aname =avalue =2onchange =document.getElementById('ac')。submit();检查= 检查 >
< input type =radioid =aaname =avalue =1onchange =document.getElementById('ac')。submit();>
< input type =radioid =aaaname =avalue =0onchange =document.getElementById('ac')。submit();>
< / form>
http://docs.python.org/2.7/library/urllib.html =nofollow noreferrer> 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-键入:application / x-www-form-urlencoded,Accept:text / plain}
h.request('POST',get_data,data,headers)
r = h。 getresponse()
print r.read()
I need to parse HTML in my python script. However to get to the page I need, in my browser, to click radio button and submit form. So, how can I submit form with python and get rendered html back? Thanks, I would appreciate any help.
The form I'm talking about:
<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>
You can use urllib for this: http://docs.python.org/2.7/library/urllib.html
A radio button sends its data as name=value, in this case a=2.
Modified example from 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()
这篇关于用python提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!