Login to google using Python
Question: I have been trying for some time now to login to my google account and
download a file from it **without using any external libraries**. I am
specifically talking about Google Finance (<https://www.google.com/finance>).
All I want to do is login, and download my portfolio (after you sign in and go
to the Portfolios tab, there is a link saying: Download to spreadsheet). But I
can't get it to work.

I have seen several posts here regarding similar problems but none of them
worked for me.

This is the code I have now:

    
    
    import urllib, urllib2, cookielib
    
    #Gets the current directory
    output_path = os.getcwd()
    
    def make_url(ticker_symbol):
        return base_url + ticker_symbol
    
    def make_filename(ticker_symbol):
        return output_path + "\\" + ticker_symbol + ".csv"
    
    # Login page for Google Finance
    login_url = "https://accounts.google.com/ServiceLogin?service=finance&passive=1209600&continue=https://www.google.com/finance&followup=https://www.google.com/finance"
    
    # Google Finance portfolio Download url (works after you signed in)
    download_url = "https://www.google.com/finance/portfolio?pid=1&output=csv&action=viewt&ei=ypZyUZi_EqGAwAP5Vg"
    
    username = 'my_username'
    password = 'my_password'
    
    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    login_data = urllib.urlencode({'username' : username, 'j_password' : password})
    opener.open(login_url, login_data)
    
    # Download the file
    try:
        urllib.urlretrieve(download_url, make_filename("My_portfolio"))
    except urllib.ContentTooShortError as e:
        outfile = open(make_filename("My_portfolio"), "w")
        outfile.write(e.content)
        outfile.close()
    

What am I doing wrong?

Thanks a lot!!


Answer: The author of the
[script](http://everydayscripting.blogspot.co.il/2009/10/python-fixes-to-
google-login-script.html) you mention has since updated the said script(s).
These scripts are designed specifically for gVoice, but can be used as an
example for your needs with gFinance I'm sure.

The new scripts are found at [GitHub](https://github.com/hillmanov/gvoice).

