问题
Python mechanize gives nested FORMs error for this code:
url = 'http://bis.zju.edu.cn/psi/'
browse = mechanize.Browser()
browse.set_handle_robots(False)
browse.open(url)
# print [n for n in browse.forms()]
# ParseError: nested FORMs
browse.select_form(name="form1") # or (nr=0)
# ParseError: nested FORMs
seq = '>seq1' + '\n' + 'MNANSSAKLGDSA'
browse['sequence'] = seq
response = browse.submit()
Neither this solves:
browse = mechanize.Browser(factory=mechanize.RobustFactory())
browse.set_handle_robots(False)
browse.open(url)
print [n for n in browse.forms()]
# [] # empty list
browse.select_form(name="form1") # or (nr=0)
# FormNotFoundError: no form matching name 'form1'
Nor this:
browse = mechanize.Browser()
browse.set_handle_robots(False)
response = browse.open(url)
# 'modify response' (anyway? to remove erroneous html?)
browse.set_response(response)
print [n for n in browse.forms()]
# [] # empty list
browse.select_form(name="form1") # or (nr=0)
# FormNotFoundError: no form matching name 'form1'
I have searched net for Q/A on nested forms error in python/mechanize in stackoverflow/ other forums. No solutions.
Any (quick) help is appreciated. Thanks.
回答1:
Take a look at this question, and possibly try using BeautifulSoup to parse the website instead of mechanize itself, BS is a little bit more robust.
来源:https://stackoverflow.com/questions/40738461/parseerror-nested-forms