Nucklear
Nucklear

Reputation: 478

Select radiobutton with python urllib2

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.

Upvotes: 1

Views: 1725

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600059

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.

Upvotes: 2

Related Questions