using openurl to log into Yahoo services

Mike Meyer mwm at mired.org
Wed Nov 16 15:20:14 EST 2005


"joe_public34" <joe_public34 at yahoo.com> writes:

> Hello all,
>
> I'm trying to write a script to log into Yahoo! (mail, groups, etc),

I don't have an answer to your problem but I *do* have a script that
logs into yahoo. It's ugly, but it works. I'm refactoring it - if I
ever get back to it.

I've appended the crucial code, but haven't tried running this
excerpt. If you have problems or questions, feel free to ask.

         <mike


myYahooID = 'Your Id Here'

from urllib2 import build_opener, HTTPCookieProcessor
from urllib import urlencode
from BeautifulSoup import BeautifulSoup
from sys import argv, exit

urlopen = build_opener(HTTPCookieProcessor()).open

def login(password):
    """Get past the multiple login forms that Yahoo uses."""

    print "Logging in to Yahoo groups."

    passage = BeautifulSoup(urlopen('http://groups.yahoo.com').read())
    passage.done()
    i = 0
    while passage and i < 5:
        page = passage
        passage = handle_login(passage, password)
        i = i + 1

    if i >= 5:
        handle_bogus(page, 'To many login attempts')	# Never returns

    while 1:
        metas = page.fetch('meta', {'http-equiv': 'Refresh'})
        if not metas:
            return page
        # Sigh. It's redirect page that didn't get handled by the library.
        page = meta_redirect(metas[0]['content'])
                    
    return page


def handle_login(soup, password):
    """Parse a page for login information, and hand back the result of logging in.

    Returns None if there is no login form."""

    # Get the login page, scrape the form, and log in!
    forms = soup.fetch('form', {'name': 'login_form'})
    if not forms:
        return None
    form = forms[0]

    postdata = []
    for intag in form.fetchNext('input'):
        if intag['type'] == 'hidden':
            postdata.append((intag['name'], intag['value']))
        elif intag['type'] == 'password':
            postdata.append((intag['name'], password))
        elif intag['type'] == 'checkbox':
            postdata.append((intag['name'], 'n'))
        elif intag['type'] == 'text' and intag['name'] == 'login':
            postdata.append(('login', myYahooID))
        elif intag['type'] == 'submit':
            if intag.has_key(['name']):
                postdata.append((intag['name'], intag['value']))
            else:
                postdata.append(('submit', intag['value']))
        else:
            print "Login form had unrecognized input tag:", str(intag)

    out = BeautifulSoup(urlopen(form['action'], urlencode(postdata)).read())
    out.done()
    return out

def meta_redirect(value):
    """Handle a http-equiv redirect, since the library isn't reliable."""

    for spam in value.split(';'):
        stuff = spam.strip().split('=')
        if stuff and stuff[0] == 'url':
            out = BeautifulSoup(urlopen(stuff[1]).read())
            out.done()
            return out
            

-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list