質問

I'm trying to use mechanize to automatically log into a website and check some figures. I'm pretty sure I've got past the first page with the usual username password form but the second login page asks for specific characters from an answer to a security question you pick during account creation.

Like if your favorite pet was called garfield and it asked for the 2nd, 4th and 5th characters you would have to fill out 3 inputs with a, f, i and post that form.

Im not sure what the best way of having mechanize search for which characters it wants each time it logs in is. The start of the source code for the form contains this.

<label for="frmentermemorableinformation1:strEnterMemorableInformation_memInfo1">Character 5 &#160;</label>

It has another 2 of those labels for the other 2 characters to be filled in on the form, I thought it would be a good idea to get mechanize to read through the source searching for the first 3 occurances of "character " then reading the character right after each occurance as the first 3 times that text comes up is for those labels.

How would i go about doing this and is there an easier way to do this rather than reading through the entire source code, can mechanize specifically search for labels within that form or some other shortcut? Also I'm not very experienced with programming so any extra detail or explanations would be great. Thank you.

役に立ちましたか?

解決

How would i go about doing this and is there an easier way to do this rather than reading through the entire source code, can mechanize specifically search for labels within that form or some other shortcut?

I'm not that familiar with mechanize in python, but with mechanize in ruby for instance, you use an html parser to search through the html. python's html parser is BeautifulSoup or libxml. BeautifulSoup is easier to install--libxml has lots of dependencies and can be a bear to install. Here is a BeautifulSoup example:

from BeautifulSoup import BeautifulSoup as bs
import re

soup = bs(open('html.html'))

form = soup.find(id="form1")

labels = form.findAll('label', text=re.compile("Character \d+") )
labels = labels[:3]

for label in labels:
    print(label.string)

--output:--
Character 5 &#160;
Character 6 &#160;
Character 7 &#160;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top