Submitting data to a form
I need to log in to a website using python. The two fields are username and password. This is what I have tried:
import urllib
import urllib2
import httplib
username = "username"
password = "password"
url = "https://webconnect.bloomfield.org/Zangle/StudentConnect/logincheck.aspx"
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, "https://webconnect.bloomfield.org/Zangle/StudentConnect/", username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
req = urllib2.Request(url)
f = opener.open(req)
print f.read()
This only returns the html code of the page with the login form, meaning the login wasn't successful. Of course when I tried, I used a valid username and password. While you can't test it out on this site, is there anything you see that is obviously the problem. I'm not very good with programming with websites, I figured out that much from tutorials...
gsingh2011
Junior Poster in Training
70 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
Thanks! That is very useful. I've spent so long trying to get other random code to work, but that was by far the easiest solution. I have one more question though. Now that I'm logged in, I need to navigate to a different page that I can only access once I'm logged in. How do I go to that page? br.open('http://websitename.com/page.aspx')? When I tried that it said my session had expired and that I had to log back in. How can I get to that page without it kicking me out? Typing that page name in the browser after I'm logged in works fine, btw. Here's the code I'm using:
import mechanize
import cookielib
# Browser
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# User-Agent (this is cheating, ok?)
br.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')]
# Open some site, let's pick a random one, the first that pops in mind:
r = br.open('https://webconnect.bloomfield.org/Zangle/StudentConnect/')
#r = br.open('http://google.com')
# Show the available forms
#for f in br.forms():
# print f
br.select_form(nr=0)
br.form['stuident']='username'
br.form['stupassword']='password'
br.submit()
br.open('https://webconnect.bloomfield.org/Zangle/StudentConnect/stuassignments.aspx') #This is where I try to open that page... It doesn't work.
print br.response().read()
gsingh2011
Junior Poster in Training
70 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
Here's an update. I tried the code on facebook, and it logged in successfuly, but it wasn't able to go to another page when I used br.open(). When I switched it to br.follow_link() it worked fine. The problem is I'm not always going to have a link on the page I want to go to, like in my original example in the code posted above. I need to be able to directly go to that url...
gsingh2011
Junior Poster in Training
70 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
It does seem like a cookie problem, but according to http://wwwsearch.sourceforge.net/mechanize/ cookies are automatically handled. And the code you posted doesn't work for me, I tried it a while ago. The mechanize code at least logs me in, its just going to another page that's the problem.
gsingh2011
Junior Poster in Training
70 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
gsingh2011
Junior Poster in Training
70 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
Could you post the exact code you used to log in to those websites? And after you log in, could you switch to a different page on that website and print the source? I hate asking for code, but this just isn't working for me...
gsingh2011
Junior Poster in Training
70 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
I tried this with facebook and demonoid, and it when I open up test.html it tells me to login Here's the facebook login source:
I'm hoping you have a facebook account so you can try it yourself... Thanks.
gsingh2011
Junior Poster in Training
70 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
I will take a look at Pyfacebook and see if I can get it too work. Also, while facebook was my original intention for my program, I want to extend it to other sites. The other site I was trying was in my first few posts. It's a school site, so you wont be able to test anything with it, but just like facebook I wasn't able to log in. What are some reasons this could be happening? Tech B, you mentioned javascript earlier....
gsingh2011
Junior Poster in Training
70 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
I wasn't able to figure it out yet. I'd rather figure out how to do a simple connection without external libraries than waste time learning how to use this library anyway... thanks for the suggestion though.
Tech B, did you read my last question?
gsingh2011
Junior Poster in Training
70 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0