I'm trying to locate search boxes on websites using the Mechanize python package to find forms on web pages. Pretty much every website defines these forms in their own way so I need to search for a bunch of different signatures. Because the Mechanize Browser.select_form function throws an exception whenever it fails to find the specified form, looking for a lot of different forms turns into a long list of try and except statements.
The first thing I tried(or rolled into) is the following structure. It works, but 1: it doesn't look very good, 2: expands badly(if I need even more statements this turns into chaos) and 3: overal this just seems like bad code.
from mechanize import Browser
br = Browser()
br.open(url)
try:
br.select_form(id=lambda x: 'search' in x)
except Exception:
try:
br.select_form(class_=lambda x: 'search' in x)
except Exception:
try:
br.select_form(action=lambda x: 'search' in x)
except Exception:
try:
br.select_form(role=lambda x: 'search' in x)
except Exception:
print('NOTHING FOUND')
pass
A possibly slightly better solution would to direct the except clauses to functions, as in https://stackoverflow.com/a/6095782/11309912. This would solve the sideways expansion but still consists of a lot of repeated code.
To me the ideal solution would be to have a list of statements I could iterate over until one type of form was found. A very crude example would be:
forms = ['id=lambda x: 'search' in x', 'class_=lambda x: 'search' in x', .....]
for form in forms:
try:
br.select_form(form)
break
except Exception:
pass
Is something similar to this possible?