使用Python登录到google



我已经尝试了一段时间,现在登录到我的谷歌帐户,并从它下载一个文件不使用任何外部库。我特别说的是谷歌金融(https://www.google.com/finance)。所有我想做的是登录,并下载我的投资组合(你登录后,去投资组合标签,有一个链接说:下载到电子表格)。但是我不能让它工作。

我在这里看到了一些关于类似问题的帖子,但没有一个对我有效。

这是我现在的代码:

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()

我做错了什么?

非常感谢!

您提到的脚本的作者已经更新了上述脚本。这些脚本是专门为gVoice设计的,但我相信可以作为gFinance的一个例子来使用。

最新更新