471,357 Members | 1,087 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

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

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)>>

Nov 5 '07 #1
9 6435
scripteaze wrote:
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
Nov 5 '07 #2
On Nov 5, 8:52 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
scripteaze wrote:
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

Nov 5 '07 #3
>
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
Nov 5 '07 #4
b 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.
Is it possible then to have a form with no name and if so, how can i
access this form

Nov 5 '07 #5
On Nov 6, 8:56 am, scripteaze <scripte...@gmail.comwrote:
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

Nov 6 '07 #6
On Nov 5, 6:33 pm, alex23 <wuwe...@gmail.comwrote:
On Nov 6, 8:56 am, scripteaze <scripte...@gmail.comwrote:
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.

Nov 6 '07 #7
On Nov 5, 6:05 pm, scripteaze <scripte...@gmail.comwrote:
[...]
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 <winkor
not...

-- bjorn

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

Nov 6 '07 #8
alex23 <wu*****@gmail.comwrites:
On Nov 6, 8:56 am, scripteaze <scripte...@gmail.comwrote:
>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
Nov 7 '07 #9
On Nov 7, 1:35 pm, j...@pobox.com (John J. Lee) wrote:
alex23 <wuwe...@gmail.comwrites:
On Nov 6, 8:56 am, scripteaze <scripte...@gmail.comwrote:
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- 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

Nov 8 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Mason A. Clark | last post: by
40 posts views Thread by aku | last post: by
2 posts views Thread by Guoqi zheng | last post: by
23 posts views Thread by Abhi | last post: by
14 posts views Thread by terrible.theterrible4 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.