Search code examples
pythonmechanizemechanize-python

Python Mechanize: Session has expired


Trying out mechanize to scrape some content off an https asp site, it looks as if the login page submission works as I get returned a 200. But when I try and open a url presumably using the cookie captured after the login, I get re-directed back to the login page with the error that my session has expired. The last print is just so I can see that I get redirected.

import mechanize 

USER_AGENT = "Mozilla/5.0 (X11; U; Linux i686; tr-TR; rv:1.8.1.9) Gecko/20071102 Pardus/2007 Firefox/2.0.0.9"

mech = mechanize.Browser()
mech.addheaders = [("User-agent", USER_AGENT)]

mech.open("https://www.example.com/login.asp")

mech.select_form("loginform")
mech['id'] = "blah"
mech['pin'] = "blah"
response = mech.submit()

trueContent = mech.open("https://www.example.com/content")

print trueContent.geturl()

Solution

  • Your code looks good to me however I don't see any check that login was successful

    response = mech.submit()
    

    Look at the content of response to make sure your login was successful.

    Are you sure this site works without javascript? There could be hidden field that are set by javascript.