Finding form input fields using Mechanize where the field's label contains a string in a list of strings?
Now I understand what the error means, but I'm curious why this doesn't work and if there is an alternative available which will complete the same task. I pass regex's when using Beautiful Soup and am disappointed to find that Mechanize does not support (?) the same functionality. input_names is a dict.
TypeError: control label must be string-like
# first - as a default - set form inputs by their labels
for k in variables:
for word in input_names[k]:
for control in br.form.find_co开发者_Python百科ntrol(label=re.compile(word)):
br.form.set_value(variables[k], name=control.name)
The goal is to match any form input whose label contains a phrase, rather than matches completely.
Also, any tips on efficiency or code prettying/elegance appreciated; I'm new to python.
Workaround: (untested)
for tag in soup.findAll("label"):
for k in variables:
# try to find label in <label>X</label>. It will not be in for="X"
# because if it was, we'd find it below anyway.
for word in input_names[k]:
if word in tag.contents.lower():
try:
br.form.find_control(name=tag['for'], kind="text").value = variables[k]
except:
print "failed to set value of input found by label."
Assuming br.form.find_control()
doesn't take regexes, we'll need to solve it somehow, e.g. by manually finding and matching all controls. Alternatively, use BS
with regex to make the match to literal text, then feed that back to mechanize
.
精彩评论