本文介绍了使用 python urllib2 选择单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python urllib2 从此表单中选择一个单选按钮并通过按钮提交表单:

<input type="radio" name="2" value="21"/>选项<br/><input type="radio" name="2" value="22"/>选项<br/><input type="radio" name="2" value="23"/>选项<br/><input type="radio" name="2" value="24"/>选项<br/><input type="radio" name="2" value="25"/>选项<br/><input type="radio" name="2" value="26"/>选项<br/><input type="radio" name="2" value="27"/>选项<br/><input type="radio" name="2" value="28"/>选项<br/><input type="radio" name="2" value="29"/>选项<br/><input type="radio" name="2" value="30"/>选项<br/><input type="radio" name="2" value="31"/>选项<br/><input type="radio" name="2" value="32"/>选项<br/><input type="radio" name="2" value="33"/>选项<br/><input type="radio" name="2" value="34"/>选项<br/><input type="radio" name="2" value="35"/>选项<br/><input type="radio" name="2" value="36"/>选项<br/><input type="radio" name="2" value="37"/>选项<br/><input type="radio" name="2" value="38"/>选项<br/><input type="radio" name="2" value="39"/>选项<br/><input type="radio" name="2" value="40"/>选项<br/>

我对这个任务有点迷茫,我发现的信息与使用机械化有关.有没有办法用 python 标准库来完成这项工作?

问候.

解决方案

您无需使用 urllib2选择"事物,因为它与浏览器没有任何关系.相反,您需要构建表单发送的数据,然后直接发布:

导入 urllib, urllib2data = urllib.urlencode({'2': '22'}) # 假设您要选择值22"请求 = urllib2.Request(my_url, 数据)结果 = urllib2.urlopen(请求)打印 result.read()

如果还有其他字段,可以将它们添加到传递给urlencode的字典中.

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.

解决方案

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.

这篇关于使用 python urllib2 选择单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 05:45