Select radiobutton with python urllib2

戏子无情 提交于 2019-12-08 13:31:33

问题


I'm trying to select a radiobuton from this form with python urllib2 and submit the form through a button:

<div id="show-form2" class="show-form2">
<input type="radio" name="2" value="21"/>
OPTION 
<br/>
<input type="radio" name="2" value="22"/>
OPTION 
<br/>
<input type="radio" name="2" value="23"/>
OPTION 
<br/>
<input type="radio" name="2" value="24"/>
OPTION 
<br/>
<input type="radio" name="2" value="25"/>
OPTION 
<br/>
<input type="radio" name="2" value="26"/>
OPTION 
<br/>
<input type="radio" name="2" value="27"/>
OPTION 
<br/>
<input type="radio" name="2" value="28"/>
OPTION 
<br/>
<input type="radio" name="2" value="29"/>
OPTION 
<br/>
<input type="radio" name="2" value="30"/>
OPTION 
<br/>
<input type="radio" name="2" value="31"/>
OPTION 
<br/>
<input type="radio" name="2" value="32"/>
OPTION 
<br/>
<input type="radio" name="2" value="33"/>
OPTION 
<br/>
<input type="radio" name="2" value="34"/>
OPTION 
<br/>
<input type="radio" name="2" value="35"/>
OPTION 
<br/>
<input type="radio" name="2" value="36"/>
OPTION 
<br/>
<input type="radio" name="2" value="37"/>
OPTION 
<br/>
<input type="radio" name="2" value="38"/>
OPTION 
<br/>
<input type="radio" name="2" value="39"/>
OPTION 
<br/>
<input type="radio" name="2" value="40"/>
OPTION 
<br/>

I'm a little lost in this task, and the info I found is related to use mechanize. Is there a way to get this done with python standard libraries?

Regards.


回答1:


You don't "select" things with urllib2, because it doesn't have anything to do with a browser. Instead you need to construct the data that would be sent by the form, and post it directly:

import urllib, urllib2
data = urllib.urlencode({'2': '22'}) # assume you want to select the value "22"
request = urllib2.Request(my_url, data)
result = urllib2.urlopen(request)
print result.read()

If there are other fields, you can add them inside the dictionary that you pass to urlencode.



来源:https://stackoverflow.com/questions/12492536/select-radiobutton-with-python-urllib2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!