Python 在Mechanize中获取br.forms()的键和值
使用Mechanize,我能够获得页面的所有形式Python 在Mechanize中获取br.forms()的键和值,python,forms,mechanize,Python,Forms,Mechanize,使用Mechanize,我能够获得页面的所有形式 for f in br.forms(): print f 对于我的页面,它为我提供如下信息: <HiddenControl(assoc_term_in=201535) (readonly)> <HiddenControl(CRN_IN=34688) (readonly)> <HiddenControl(start_date_in=03/28/2016) (readonly)> <Hi
for f in br.forms():
print f
对于我的页面,它为我提供如下信息:
<HiddenControl(assoc_term_in=201535) (readonly)>
<HiddenControl(CRN_IN=34688) (readonly)>
<HiddenControl(start_date_in=03/28/2016) (readonly)>
<HiddenControl(end_date_in=06/11/2016) (readonly)>
<HiddenControl(SUBJ=ECEC) (readonly)>
<HiddenControl(CRSE=451) (readonly)>
<HiddenControl(SEC=001) (readonly)>
<HiddenControl(LEVL=Undergraduate Quarter) (readonly)>
<HiddenControl(CRED= 3.000) (readonly)>
<HiddenControl(GMOD=Standard Letter) (readonly)>
<HiddenControl(TITLE=Computer Arithmetic) (readonly)>
<HiddenControl(MESG=DUMMY) (readonly)>
<SelectControl(RSTS_IN=[*, WR])>
<HiddenControl(assoc_term_in=201535) (readonly)>
<HiddenControl(CRN_IN=31109) (readonly)>
<HiddenControl(start_date_in=03/28/2016) (readonly)>
<HiddenControl(end_date_in=06/11/2016) (readonly)>
<HiddenControl(SUBJ=BIO) (readonly)>
<HiddenControl(CRSE=141) (readonly)>
<HiddenControl(SEC=073) (readonly)>
<HiddenControl(LEVL=Undergraduate Quarter) (readonly)>
<HiddenControl(CRED= 0.000) (readonly)>
<HiddenControl(GMOD=Non Gradeable Unit) (readonly)>
<HiddenControl(TITLE=Essential Biology) (readonly)>
<HiddenControl(MESG=DUMMY) (readonly)>
<SelectControl(RSTS_IN=[*, WD])>
我试过使用f.value
,f.value
,f['TITLE']
,但运气不好
我以前就有这个功能,但是当我删除注释将代码提交到版本控制时,我丢失了代码,如果您只需要一个特定的值,并且知道密钥:
In [18]: response = br.open("http://www.w3schools.com/html/html_forms.asp")
In [19]: f = list(br.forms())
In [20]: f[0].get_value("firstname")
Out[20]: 'Mickey'
In [21]: f[0].get_value("lastname")
Out[21]: 'Mouse'
您可以使用f.\u pairs()
访问所有对:
您可以看到它为您提供了键、值对:
<GET http://www.w3schools.com/html/action_page.php application/x-www-form-urlencoded
<TextControl(firstname=Mickey)>
<TextControl(lastname=Mouse)>
<SubmitControl(<None>=Submit) (readonly)>>
[('firstname', 'Mickey'), ('lastname', 'Mouse')]
<GET http://www.w3schools.com/html/action_page.php application/x-www-form-urlencoded
<TextControl(firstname=Mickey)>
<TextControl(lastname=Mouse)>
<SubmitControl(<None>=Submit) (readonly)>>
[('firstname', 'Mickey'), ('lastname', 'Mouse')]
<GET http://www.w3schools.com/html/html_forms.asp application/x-www-form-urlencoded
<TextControl(err_email=)>
<TextControl(err_url=) (disabled)>
<TextareaControl(err_desc=)>
<IgnoreControl(<None>=<None>)>>
[('err_email', ''), ('err_desc', '')]
[('firstname','Mickey'),('lastname','Mouse')]
[('firstname','Mickey'),('lastname','Mouse')]
[('err_email','')('err_desc','')]
谢谢!我会尝试一下,然后再给你回复!嗨,帕德雷克。所以我看到f.pairs()
是一个元组,所以我必须使用索引来访问它们<代码>用于br.forms()中的f:f.\u对中的p():打印p[1]。这是目前我访问元组中的值的一种方法,但是,这会打印所有内容。我想指定键,所以我将br.forms()
和f.pairs()
都转换为list()
,但我得到了以下错误:元组索引必须是整数,而不是str
@thegreencolla,如果您知道键以及键的形式,那么就调用表单get\u值(“键”)
将在表单上获取所需的值。问题是,有多个键具有相同的名称,因此返回此错误:多个控件匹配名称“CRN\u IN”
。我决定使用double for循环,看看是否可以循环不同的控件,但运气不好。@TheGreenColla,你能分享链接吗?
for f in br.forms():
print(f._pairs())
response = br.open("http://www.w3schools.com/html/html_forms.asp")
for f in br.forms():
print(f)
print(f._pairs())
<GET http://www.w3schools.com/html/action_page.php application/x-www-form-urlencoded
<TextControl(firstname=Mickey)>
<TextControl(lastname=Mouse)>
<SubmitControl(<None>=Submit) (readonly)>>
[('firstname', 'Mickey'), ('lastname', 'Mouse')]
<GET http://www.w3schools.com/html/action_page.php application/x-www-form-urlencoded
<TextControl(firstname=Mickey)>
<TextControl(lastname=Mouse)>
<SubmitControl(<None>=Submit) (readonly)>>
[('firstname', 'Mickey'), ('lastname', 'Mouse')]
<GET http://www.w3schools.com/html/html_forms.asp application/x-www-form-urlencoded
<TextControl(err_email=)>
<TextControl(err_url=) (disabled)>
<TextareaControl(err_desc=)>
<IgnoreControl(<None>=<None>)>>
[('err_email', ''), ('err_desc', '')]