Dustin Wyatt
Dustin Wyatt

Reputation: 4244

How to make mechanize not fail with forms on this page?

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

Upvotes: 4

Views: 2451

Answers (2)

Yuda Prawira
Yuda Prawira

Reputation: 12461

Use this secret, i'm sure this is work for you ;)

br = mechanize.Browser(factory=mechanize.DefaultFactory(i_want_broken_xhtml_support=True))

Upvotes: 2

Boris Gu&#233;ry
Boris Gu&#233;ry

Reputation: 47585

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

Upvotes: 2

Related Questions