how to login to Yahoo programatically from an ubuntu server

Viewed 3893

I would like to login to my yahoo account from a script running on an ubuntu server. I have tried to use python with mechanize, but there is a flaw in my plan.

This is the code I have at the moment.

        loginurl = "https://login.yahoo.com/config/login"
        br = mechanize.Browser()
        cj = cookielib.LWPCookieJar()
        br.set_cookiejar(cj)
        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)
        br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
        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')]
        r = br.open(loginurl)
        html = r.read()
        br.select_form(nr=0)
        br.form['login']='[mylogin]'
        br.form['passwd']='[mypassword]'
        br.submit()

        print br.response().read()

The response I get is a Yahoo login page with bold red text reading. "Javascript must be enabled on your broswer" or something similar. There is a section on the mechanize docs that mentions pages that create cookies with JS, but alas the help page returns a HTTP 400 (just my luck)

Figuring out what the javascript does and then doing it manually sounds like a very difficult task. I would be quite willing to switch to any tool / language, so long as it can run on an ubuntu server. Even if it means using a different tool for the login and then passing the login cookie back to my python script. Any help / advice appreciated.

Update:

  • I do not wish to use the Yahoo APIs

  • I have also tried with scrapy, but I think the same issue occurs

My scrapy script

class YahooSpider(BaseSpider):
name = "yahoo"
start_urls = [
    "https://login.yahoo.com/config/login?.intl=us&.lang=en-US&.partner=&.last=&.src=&.pd=_ver%3D0%26c%3D%26ivt%3D%26sg%3D&pkg=&stepid=&.done=http%3a//my.yahoo.com"
]

def parse(self, response):
    x = HtmlXPathSelector(response)
    print x.select("//input/@value").extract()
    return [FormRequest.from_response(response,
                formdata={'login': '[my username]', 'passwd': '[mypassword]'},
                callback=self.after_login)]

def after_login(self, response):
    # check login succeed before going on
    if response.url == 'http://my.yahoo.com':
        return Request("[where i want to go next]",
                  callback=self.next_page, errback=self.error, dont_filter=True)
    else:
        print response.url
        self.log("Login failed.", level=log.CRITICAL)

def next_page(sekf, response):
    x = HtmlXPathSelector(response)
    print x.select("//title/text()").extract()

The scrapy script just outputs "https://login.yahoo.com/config/login" ...... boo

6 Answers
Related