python mechanize forms() err

亡梦爱人 提交于 2019-12-06 13:16:49

The URL you are trying to open is GZipped (check it using this link), So you have to append Accept-Encoding header for gzip to your Browser:

import mechanize
br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Firefox')]
br.addheaders.append( ['Accept-Encoding','gzip'] )
br.open("http://dining.ut.ac.ir/")
br.forms()

I had faced this same issue earlier and this line of code fixed my problem:

br = mechanize.Browser(factory=mechanize.RobustFactory())

So, try with this:

import mechanize
br = mechanize.Browser(factory=mechanize.RobustFactory())
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Firefox')]    
br.open("http://dining.ut.ac.ir/")
br.forms
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!