一段时间以来,我一直在尝试登录我的 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()
我究竟做错了什么?
非常感谢!!