mechanize: first form works, then “unknown GET form encoding type 'utf-8'”

六月ゝ 毕业季﹏ 提交于 2019-12-13 05:15:12

问题


I am trying to fill out 2 forms from the EUR-Lex website in order to record some data from the generated webpage. I am stuck at form #2. I get the feeling this should be easy and I've researched a bit, but no luck.

import mechanize

froot = '...'
f = open(froot + 'text.html', 'w')

br = mechanize.Browser()
br.open('http://eur-lex.europa.eu/RECH_legislation.do')
br.select_form(name='form2')
br['T1'] = ['V112']
br['T3'] = ['V2']
br['T2'] = ['V1']

first_page = br.submit()
f.write(first_page.get_data())

up until here everything seems to work, because I get the source of the correct page saved to the file. But then...

br.select_form(name='form2')
br['typedate'] = ['PD']

br['startaaaa'] = '1960'
br['startmm'] = '01'
br['startjj'] = '01'
br['endaaaa'] = '1960'
br['endmm'] = '12'
br['startjj'] = '31'

next = br.submit()

here everything stops:

ValueError: unknown GET form encoding type 'utf-8'

I checked br.enctype before selecting the first and second forms. What I get is:

  • after the first form: application/x-www-form-urlencoded
  • after the second form: utf-8

I don't know what is going on here.


回答1:


How I got past this problem:

I re-installed mechanize after changing the source

line 3233 of _form.py:

if (self.enctype != "application/x-www-form-urlencoded") and (self.enctype != "utf-8"):

it's probably very wrong and can only probably handle my case. but in my specific case it works.



来源:https://stackoverflow.com/questions/19099120/mechanize-first-form-works-then-unknown-get-form-encoding-type-utf-8

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