Created
December 20, 2010 13:44
-
-
Save eikes/748390 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import urllib, urllib2 | |
from BeautifulSoup import BeautifulSoup | |
def __main__(): | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) | |
urllib2.install_opener(opener) | |
url = "http://rails-app.example.com/" # this is the url of the rails app | |
request = opener.open(url + "login") # login page | |
soup = BeautifulSoup(request.read()) | |
request.close() | |
form = soup.find(id='new_user_session') #this is the id of the login form | |
auth_hash = form.find(attrs={'name':'authenticity_token'})['value'] | |
action_url = form['action'] | |
user_name = "my_user_name" | |
password = "my_password" | |
data = {'user_session[username]': user_name, 'user_session[password]': password, 'authenticity_token':auth_hash} | |
# Login: | |
request = opener.open(action_url, urllib.urlencode(data)) | |
request.read() | |
request.close() | |
# Private Page: | |
request = opener.open(url + "/messages") # private page | |
soup = BeautifulSoup(request.read()) | |
request.close() | |
print soup.find(id="messages") # do something with your private data... | |
__main__() | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment