Skip to content

Instantly share code, notes, and snippets.

@pariser
Created November 1, 2011 18:09
Show Gist options
  • Select an option

  • Save pariser/1331398 to your computer and use it in GitHub Desktop.

Select an option

Save pariser/1331398 to your computer and use it in GitHub Desktop.
Y!Login
#!/usr/bin/python
import urllib
import urllib2
import re
import json
import os
from pyquery import PyQuery
import codecs
# Set default user agent
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 Firefox/7.0.1')]
# Load the fantasy football page
usock = opener.open('http://football.fantasysports.yahoo.com/')
html = unicode(usock.read(), 'utf-8')
usock.close()
# Find the Sign In link
d = PyQuery(html)
header = d("#yuhead-mepanel")
signup_url = header.find('a').filter(lambda i: 'login.yahoo.com' in PyQuery(this).attr('href')).attr('href')
# Load the Y! Login page
usock = opener.open(signup_url)
html = unicode(usock.read(), 'utf-8')
usock.close()
# Read the login form
d = PyQuery(html)
login_form = d("#login_form")
form_action = login_form.attr('action')
login_dict = dict(login_form.find('input').map(lambda i,e: (PyQuery(e).attr('name'), PyQuery(e).attr('value'))))
# Insert credentials
login_dict['login'] = 'USERNAMEHERE'
login_dict['passwd'] = 'PASSWORDHERE'
login_data = urllib.urlencode(login_dict)
# Login
usock = opener.open(form_action, login_data)
html = unicode(usock.read(), 'utf-8')
usock.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment