dimanche 22 novembre 2015

Python mechanize form submission not working

I'm trying to put together a python program that will periodically gather course data from a university website and save them to a csv file for personal use. After some searching around I stumbled across mechanize. I'm trying to set it up so I can log into my account but I've run into a stumbling block. The code I put together here is supposed to submit the form containing the login information. The website is http://ift.tt/1lCVeaV. The response page is supposed to display a red error message when a form is submitted with the wrong log in information. The response I keep getting lacks such an error message and I cant figure out what I'm doing wrong.

import mechanize
from bs4 import BeautifulSoup
import cookielib
import urllib2

# log in page url
url = "http://ift.tt/1lCVeaV"

# create the browser object and give it fake headers
myBrowser = mechanize.Browser()
myBrowser.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
myBrowser.set_handle_robots(False)

# have the browser open the site
myBrowser.open(url)

# handle the cookies
cj = cookielib.LWPCookieJar()
myBrowser.set_cookiejar(cj)

# select third form down
myBrowser.select_form(nr=2)

# fill out the form
myBrowser["j_username"] = "somestudentusername"
myBrowser["pwd"] = "somestudentpassword"

# submit the form and save the response
response = myBrowser.submit()

# upon the submission of incorrect login information an error message is displayed in red
soup = BeautifulSoup(response.read(), 'html.parser')
print (soup.find(color="red")) #find the error message in the response




Aucun commentaire:

Enregistrer un commentaire