【问题标题】:Download data from Yahoo! Finance从雅虎下载数据!金融
【发布时间】:2018-09-24 19:05:51
【问题描述】:

您好,我最近接手了一个关于比特币分析的项目,需要从 Yahoo! 下载财务数据!通过 Python 进行财务。我尝试了 fix_yahoo_finance 和 pandas datareader,但下载文件时网站上似乎存在错误。它总是错过一些日子。所以我决定用美汤,代码如下:

import requests
import time
import pandas as pd
from bs4 import BeautifulSoup

def time_convert(dt):
    time.strptime(dt,'%Y-%m-%d %H:%M:%S')
    s = time.mktime(time.strptime(dt,'%Y-%m-%d %H:%M:%S'))
    return str(int(s))

s = requests.Session()
start = time_convert("2016-02-15 00:00:00")
end   = time_convert("2018-02-15 00:00:00")

r = s.get("https://uk.finance.yahoo.com/quote/BTC-USD/history?period1="+start+"&period2="+end+"&interval=1d&filter=history&frequency=1d"

soup = BeautifulSoup(r.text, 'lxml')
tables = soup.select('table')

df_list = []
for table in tables:
    df_list.append(pd.concat(pd.read_html(table.prettify())))
    df = pd.concat(df_list)
    df.to_excel("E:\PythonData\price_"+'.xlsx')

它可以工作,但数据不完整,因为当您的鼠标向下滚动到页面末尾时,网站会加载数据,但代码不会这样做。我该如何解决这个问题?

【问题讨论】:

    标签: python pandas beautifulsoup yahoo-finance


    【解决方案1】:

    您是否尝试过使用 Yahoo Financials?它构建得非常好,不会废弃网页。它从 ["context"]["dispatcher"]["stores"] 对象中散列出您想要的数据。它速度很快,而且构建得非常好。

    $ pip install yahoofinancials

    用法示例:

    from yahoofinancials import YahooFinancials
    
    tech_stocks = ['AAPL', 'MSFT', 'INTC']
    bank_stocks = ['WFC', 'BAC', 'C']
    
    yahoo_financials_tech = YahooFinancials(tech_stocks)
    yahoo_financials_banks = YahooFinancials(bank_stocks)
    
    tech_cash_flow_data_an = yahoo_financials_tech.get_financial_stmts('annual', 'cash')
    bank_cash_flow_data_an = yahoo_financials_banks.get_financial_stmts('annual', 'cash')
    
    banks_net_ebit = yahoo_financials_banks.get_ebit()
    tech_stock_price_data = tech_cash_flow_data.get_stock_price_data()
    daily_bank_stock_prices = yahoo_financials_banks.get_historical_stock_data('2008-09-15', '2017-09-15', 'daily')
    

    输出示例:

    yahoo_financials = YahooFinancials('WFC')
    print(yahoo_financials.get_historical_stock_data("2017-09-10", "2017-10-10", "monthly"))
    

    返回

    {
        "WFC": {
            "prices": [
                {
                    "volume": 260271600,
                    "formatted_date": "2017-09-30",
                    "high": 55.77000045776367,
                    "adjclose": 54.91999816894531,
                    "low": 52.84000015258789,
                    "date": 1506830400,
                    "close": 54.91999816894531,
                    "open": 55.15999984741211
                }
            ],
            "eventsData": [],
            "firstTradeDate": {
                "date": 76233600,
                "formatted_date": "1972-06-01"
            },
            "isPending": false,
            "timeZone": {
                "gmtOffset": -14400
            },
            "id": "1mo15050196001507611600"
        }
    }
    

    【讨论】:

      【解决方案2】:

      Yahoo 曾经有一个财务 API,但他们已经终止了它,不过有一个解决方法。

      我之前成功使用过this,你可能想看看。

      【讨论】:

      • 先谢谢你。我下载了这个包,但不明白如何调用它的功能。你能举个例子吗?
      • 像任何其他 python 库一样运行设置脚本,导入它并使用 load_yahoo_quote 函数,ticker 是一些字符串,在你的情况下可能是“BTC-USD”,begindate 和 enddate 是不言自明的。
      • 成功了,这个函数叫做“load_yahoo_quote”。但是下载的数据是一系列的列表,能不能告诉我怎么转成dataframe?非常感谢!
      • idk 你想要什么,但谷歌是你最好的朋友,如果你只想要一个 excel 文件,csv 就足够了stackoverflow.com/questions/14037540/…
      • 实际上是一个字符串列表,每个字符串都是一行所有数据,包括价格和日期,用“,”分隔。我想知道是否有办法将数据传输到数据帧中......
      猜你喜欢
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多