首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python - yahoo finance自动下载

Python - yahoo finance自动下载
EN

Stack Overflow用户
提问于 2019-10-29 18:27:50
回答 1查看 345关注 0票数 0

我试着从这里运行代码:

Download history stock prices automatically from yahoo finance in python

代码语言:javascript
复制
import urllib

base_url = "http://ichart.finance.yahoo.com/table.csv?s="
def make_url(ticker_symbol):
    return base_url + ticker_symbol

output_path = "C:/Users/test/Desktop/research/stock_data/test"
def make_filename(ticker_symbol, directory="S&P"):
    return output_path + "/" + directory + "/" + ticker_symbol + ".csv"

def pull_historical_data(ticker_symbol, directory="S&P"):
    try:
        urllib.urlretrieve(make_url(ticker_symbol), make_filename(ticker_symbol, directory))
    except urllib.ContentTooShortError as e:
        outfile = open(make_filename(ticker_symbol, directory), "w")
        outfile.write(e.content)
        outfile.close()




pull_historical_data('AAPL', directory="S&P")

但我得到以下错误:

代码语言:javascript
复制
AttributeError: module 'urllib' has no attribute 'ContentTooShortError'

我怎么才能让它工作呢?或者你能告诉我一些可以工作的代码吗?

EN

回答 1

Stack Overflow用户

发布于 2019-10-29 18:42:48

你需要导入'urllib.request‘而不是urllib,并按照下面的说明更新你的代码。

代码语言:javascript
复制
urllib.request.urlretrieve(make_url(ticker_symbol), make_filename(ticker_symbol, directory))

except urllib.request.ContentTooShortError as e:
代码语言:javascript
复制
import urllib.request

base_url = "http://ichart.finance.yahoo.com/table.csv?s="
def make_url(ticker_symbol):
    return base_url + ticker_symbol

output_path = "C:/Users/test/Desktop/research/stock_data/test"
def make_filename(ticker_symbol, directory="S&P"):
    return output_path + "/" + directory + "/" + ticker_symbol + ".csv"

def pull_historical_data(ticker_symbol, directory="S&P"):
    try:
        urllib.request.urlretrieve(make_url(ticker_symbol), make_filename(ticker_symbol, directory))
    except urllib.request.ContentTooShortError as e:
        outfile = open(make_filename(ticker_symbol, directory), "w")
        outfile.write(e.content)
        outfile.close()

pull_historical_data('AAPL', directory="S&P")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58605579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档