How can i find the form name without "nr=0"

S

scripteaze

Im using mechanize method for retrieving the form so that i may log
into it. I need to find a way to get the form name. Its not listed
anywhere in the html source.The reason i need to do this is because im
tryin not to use the for loop below. Someone told me that the form
name should be listed in the 'print form' portion of the codes output.
I dont believe the form name is listed there also. Any ideas/help
would be great. Thank you in advance for your time.

for form in self._br.forms():
print form
self._br.select_form(nr=0)
self._br['username'] = Crawler.usrname
self._br['password'] = line.strip()
response=self._br.submit()
if 'incorrect' in response.read():
print 'password incorrect =', line.strip()


this is the output of 'print form'
--------------------------------------------------------
<POST https://somesite.com/login.html application/x-www-form-
urlencoded
<TextControl(username=)>
<PasswordControl(password=)>
<SubmitControl(<None>=Secure Login) (readonly)>
<HiddenControl(dest=inbox.ws) (readonly)>>
 
D

Diez B. Roggisch

scripteaze said:
Im using mechanize method for retrieving the form so that i may log
into it. I need to find a way to get the form name. Its not listed
anywhere in the html source.The reason i need to do this is because im
tryin not to use the for loop below. Someone told me that the form
name should be listed in the 'print form' portion of the codes output.
I dont believe the form name is listed there also. Any ideas/help
would be great. Thank you in advance for your time.

for form in self._br.forms():
print form
self._br.select_form(nr=0)
self._br['username'] = Crawler.usrname
self._br['password'] = line.strip()
response=self._br.submit()
if 'incorrect' in response.read():
print 'password incorrect =', line.strip()

How do you expect the form to be named if there is no name given in the HTML
source?

Diez
 
S

scripteaze

scripteaze said:
Im using mechanize method for retrieving the form so that i may log
into it. I need to find a way to get the form name. Its not listed
anywhere in the html source.The reason i need to do this is because im
tryin not to use the for loop below. Someone told me that the form
name should be listed in the 'print form' portion of the codes output.
I dont believe the form name is listed there also. Any ideas/help
would be great. Thank you in advance for your time.
for form in self._br.forms():
print form
self._br.select_form(nr=0)
self._br['username'] = Crawler.usrname
self._br['password'] = line.strip()
response=self._br.submit()
if 'incorrect' in response.read():
print 'password incorrect =', line.strip()

How do you expect the form to be named if there is no name given in the HTML
source?

Diez- Hide quoted text -

- Show quoted text -

Well, i wasnt sure if you could have a form without a form name, i was
just thinking that it had one but maybe hidden and that i could
retrieve it
 
D

Diez B. Roggisch

Well, i wasnt sure if you could have a form without a form name, i was
just thinking that it had one but maybe hidden and that i could
retrieve it

How hidden? HTML source is ... THE source. there is nothing hidden in there.

Diez
 
S

scripteaze

b> > Well, i wasnt sure if you could have a form without a form name,
i was
How hidden? HTML source is ... THE source. there is nothing hidden in there.
Is it possible then to have a form with no name and if so, how can i
access this form
 
A

alex23

Is it possible then to have a form with no name and if so, how can i
access this form

Hey scripteaze,

I'm not sure about mechanize, but you might have more success using
another one of the author's modules, ClientForm: http://wwwsearch.sourceforge.net/ClientForm/

from urllib2 import urlopen
from ClientForm import ParseResponse

response = urlopen("http://wwwsearch.sourceforge.net/ClientForm/
example.html")
forms = ParseResponse(response, backwards_compat=False)
form = forms[0]

As it returns a list of forms, you don't need to have a name to access
it.

Hope this helps.

-alex23
 
S

scripteaze

Is it possible then to have a form with no name and if so, how can i
access this form

Hey scripteaze,

I'm not sure about mechanize, but you might have more success using
another one of the author's modules, ClientForm:http://wwwsearch.sourceforge.net/ClientForm/

from urllib2 import urlopen
from ClientForm import ParseResponse

response = urlopen("http://wwwsearch.sourceforge.net/ClientForm/
example.html")
forms = ParseResponse(response, backwards_compat=False)
form = forms[0]

As it returns a list of forms, you don't need to have a name to access
it.

Hope this helps.

-alex23

Thank you very much for your reply. Ill check it out.
 
T

thebjorn

Well, i wasnt sure if you could have a form without a form name, i was
just thinking that it had one but maybe hidden and that i could
retrieve it

I see you've got the answer you wanted already, but just for
completeness: the following is a sufficiently(*) valid form in html

<form>
<input type=text name=q>
<input type=submit>
</form>

which will have a textbox, and a submit button with localized text
saying "Submit Query". If you type something into the textbox and hit
the button, a GET request is sent to the same "page" with ?q=something
appended to the url.

You can do the same with POSTed forms:

<form method=post>
<input type=hidden name=cmd value="rm -rf /">
<input type=submit value=Erase?>
</form>

in this case only a button with the text "Erase?" is visible.

I'm not expressing an opinion on whether this is good form <wink> or
not...

-- bjorn

(*) the HTML spec says that the action attribute is required, so
theoretically you must include it.
 
J

John J. Lee

alex23 said:
Is it possible then to have a form with no name and if so, how can i
access this form

Hey scripteaze,

I'm not sure about mechanize, but you might have more success using
another one of the author's modules, ClientForm: http://wwwsearch.sourceforge.net/ClientForm/

from urllib2 import urlopen
from ClientForm import ParseResponse

response = urlopen("http://wwwsearch.sourceforge.net/ClientForm/
example.html")
forms = ParseResponse(response, backwards_compat=False)
form = forms[0]

As it returns a list of forms, you don't need to have a name to access
it.

mechanize forms are ClientForm forms.

Quoting from mechanize.Browser.select_form().__doc__:

"""
Another way to select a form is to assign to the .form attribute. The
form assigned should be one of the objects returned by the .forms()
method.
"""

forms = list(br.forms())
br.form = pick_a_form(forms, br.global_form())


The "global form" (couldn't think of a better term) consists of all
form controls not contained in any FORM element.


John
 
S

scripteaze

Hey scripteaze,
I'm not sure about mechanize, but you might have more success using
another one of the author's modules, ClientForm:http://wwwsearch.sourceforge.net/ClientForm/
from urllib2 import urlopen
from ClientForm import ParseResponse
response = urlopen("http://wwwsearch.sourceforge.net/ClientForm/
example.html")
forms = ParseResponse(response, backwards_compat=False)
form = forms[0]
As it returns a list of forms, you don't need to have a name to access
it.

mechanize forms are ClientForm forms.

Quoting from mechanize.Browser.select_form().__doc__:

"""
Another way to select a form is to assign to the .form attribute. The
form assigned should be one of the objects returned by the .forms()
method.
"""

forms = list(br.forms())
br.form = pick_a_form(forms, br.global_form())

The "global form" (couldn't think of a better term) consists of all
form controls not contained in any FORM element.

John- Hide quoted text -

- Show quoted text -

Thank you guys for replying, im sure that one of these methods will
suffice, after this project, im definatly going to do more in lui of
web apps for learning..Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top