【发布时间】: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