basic mechanize help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick C
    New Member
    • Apr 2007
    • 54

    basic mechanize help

    I'm trying to learn the basics of the mechanize module and i'm very very new to programming.
    Does anyone know of some good places to learn the very basics, say with some examples as well?

    I found this off of a person's blog
    Code:
    import re
    from mechanize import Browser
    
    username = "yourusername"
    password = "yourpassword"
    
    browser = Browser()
    url = "http://www.deviantrealms.com/index.php?act=Login&CODE=00"
    browser.open(url)
    browser.select_form('LOGIN')
    browser['UserName'] = username
    browser['PassWord'] = password
    response = browser.submit()
    response = browser.follow_link(url_regex=r"\s*Top200-L2.php")
    browser.back()
    response = browser.follow_link(url_regex=r"\s*Top100-L2.php")
    This looks like what i think I want to do. that is sign into a web site.

    However, does the site need to be set up such that 'LOGIN' is specified just like that in the source code?

    when i tried it i get this error:
    >>> browser.select_ form('LOGIN')
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in <module>
    File "C:\Python25\li b\site-packages\mechan ize\_mechanize. py", line 510, in select_form
    raise FormNotFoundErr or("no form matching "+descripti on)
    FormNotFoundErr or: no form matching name 'LOGIN'

    thanks
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    That's usually due to your URL. If you can browse there in your web browser and get to that form, then it should work.


    The Mechanize Readme refers to some tutorials.

    Comment

    • Silgd1
      New Member
      • Sep 2007
      • 25

      #3
      Originally posted by Patrick C
      I'm trying to learn the basics of the mechanize module and i'm very very new to programming.
      Does anyone know of some good places to learn the very basics, say with some examples as well?

      I found this off of a person's blog
      Code:
      import re
      from mechanize import Browser
      
      username = "yourusername"
      password = "yourpassword"
      
      browser = Browser()
      url = "http://www.deviantrealms.com/index.php?act=Login&CODE=00"
      browser.open(url)
      browser.select_form('LOGIN')
      browser['UserName'] = username
      browser['PassWord'] = password
      response = browser.submit()
      response = browser.follow_link(url_regex=r"\s*Top200-L2.php")
      browser.back()
      response = browser.follow_link(url_regex=r"\s*Top100-L2.php")
      This looks like what i think I want to do. that is sign into a web site.

      However, does the site need to be set up such that 'LOGIN' is specified just like that in the source code?

      when i tried it i get this error:
      >>> browser.select_ form('LOGIN')
      Traceback (most recent call last):
      File "<interacti ve input>", line 1, in <module>
      File "C:\Python25\li b\site-packages\mechan ize\_mechanize. py", line 510, in select_form
      raise FormNotFoundErr or("no form matching "+descripti on)
      FormNotFoundErr or: no form matching name 'LOGIN'

      thanks

      [Silgd1] View the source code of the site and make sure that the name of the form is 'LOGIN'. From the error you are receiving, it look likes the name of the form is not 'LOGIN'. View the source code of the site again and double check the name of the form. You'll be looking for something like this <FORM name=fileupload action='/emkt/xml/submit' method=post
      encType=multipa rt/form-data target="_top">. ..this tells you the name of the form is "fileupload ". Look for something similar to this - Silgd1[/Silgd1]

      Comment

      Working...