python - 从网站查询3年的数据但只能看到三个月
问题描述
我需要分析三年的历史股票并从网站获取数据:“ https://finance.yahoo.com/quote/AAPL/history?period1=1476255600&period2=1570863600&interval=1d&filter=history&frequency=1d ” 但是,我检查了数据框架只给了3个月。
我试图改变不同的数据周期,我确实看到了网站的变化。但是,无论时间段有什么变化,我仍然可以获得 3 个月的数据。我使用 url 请求如下:
url="https://finance.yahoo.com/quote/AAPL/historyperiod1=1476255600&period2=1570863600&interval=1d&filter=history&frequency=1d " #打开链接 html = urlopen(url) 汤 = BeautifulSoup(html)
data = []
allrows= soup.find_all("tr")
for row in allrows :
row_list = row.find_all("td")
dataRow= []
for cell in row_list:
dataRow.append(cell.text)
data.append(dataRow)
data = data[6:]
#Check data head and tail to ensure the dataframe is correct
df.columns = header_list
print(df.head())
print(df.tail())
----------
Date Open High Low Close* Adj Close** Volume
0 Oct 04, 2019 225.64 227.49 223.89 227.01 227.01 34,619,700
1 Oct 03, 2019 218.43 220.96 215.13 220.82 220.82 28,606,500
2 Oct 02, 2019 223.06 223.58 217.93 218.96 218.96 34,612,300
3 Oct 01, 2019 225.07 228.22 224.20 224.59 224.59 34,805,800
4 Sep 30, 2019 220.90 224.58 220.79 223.97 223.97 25,977,400
Date Open High
Low \
91 May 29, 2019 176.42 179.35
176.00
92 May 28, 2019 178.92 180.59
177.91
93 May 24, 2019 180.20 182.14
178.62
94 May 23, 2019 179.80 180.54
177.81
95 *Close price adjusted for splits.**Adjusted cl... None None
None
Close* Adj Close** Volume
91 177.38 176.71 28,481,200
92 178.23 177.56 27,948,200
93 178.97 178.29 23,714,700
94 179.66 178.98 36,529,700
95 None None None
#check data shape
df.shape
(96, 7)
解决方案
该页面在向下滚动时显示新数据。因为 BeautifulSoup 没有执行“滚动”,所以您只能看到数据的第一部分。
如何处理?您可以使用浏览器开发工具中的“网络”选项卡查看多个数据请求。因此,您可以尝试模仿它们并直接从 API 获取数据,或者使用无头浏览器打开页面,滚动 N 次并解析页面中的所有数据。
还要仔细检查页面源——所有数据可能已经存在于 JSON 或隐藏的 HTML 元素中。
推荐阅读
- reactjs - Solana React TypeError:this.wallet.signTransaction 不是函数
- python - ValueError at / 视图 Leads.views.home_page 未返回 HttpResponse 对象。它返回 None 而不是 python django 模板错误如何修复
- qt - qRubberBand 在某些情况下不连续绘制
- visual-studio-code - VS 代码集成终端损坏
- c# - 由于 SHA256 MD5,我无法连接 posgresql db
- flutter - 从 firebase 云打印消息(数据)
- python - 自动调整 tkintertable 中的列
- reactjs - 服务器发送新数据后重新渲染反应组件
- node.js - Sequelize typescript 不将数据保存到表大小和通过 HasOne 和 HasMany 连接到模型的日志
- pygame - 如何在 Pygame 中播放视频