views:

431

answers:

1
import mechanize

url = 'http://steamcommunity.com'

br=mechanize.Browser(factory=mechanize.RobustFactory())

br.open(url)
print br.request
print br.form
for each in br.forms():
    print each
    print

The above code results in:

Traceback (most recent call last):
  File "./mech_test.py", line 12, in <module>
    for each in br.forms():
  File "build/bdist.linux-i686/egg/mechanize/_mechanize.py", line 426, in forms
  File "build/bdist.linux-i686/egg/mechanize/_html.py", line 559, in forms
  File "build/bdist.linux-i686/egg/mechanize/_html.py", line 228, in forms
mechanize._html.ParseError

My specific goal is to use the login form, but I can't even get mechanize to recognize that there are any forms. Even using what I think is the most basic method of selecting any form, br.select_form(nr=0), results in the same traceback. The form's enctype is multipart/form-data if that makes a difference.

I guess that all boils down to a two part question: How can I get mechanize to work with this page, or if it's not possible, what's another way while maintaining cookies?

edit: As mentioned below, this redirects to 'https://steamcommunity.com'.

Mechanize can successfully retrieving the HTML as can be seen with the following code:

url = 'https://steamcommunity.com'

hh = mechanize.HTTPSHandler()  # you might want HTTPSHandler, too
hh.set_http_debuglevel(1)
opener = mechanize.build_opener(hh)
response = opener.open(url)
contents = response.readlines()

print contents
+1  A: 

Did you mention that the website is redirecting to an https (ssl) server ?

Well, try to set a new HTTPS handler like this:

mechanize.HTTPSHandler()
Boris Guéry
I did forget to add that information, thanks. Unfortunately, adding the line you mention doesn't change anything.
Therms