我试图在此 url 上仅使用第二个数据帧,但我无法弄清楚如何指定仅获取一个数据帧。它打印出图表和表格的数据框,但我只想打印出表格。
`import pandas as pd
import urllib.request
page = pd.read_html('https://www.google.com/finance/historical?q=a&startdate=Jan%201%2C%202000&enddate=Feb%2028%2C%202017&num=200&ei=_nm3WKGHCIf7jAG74ar4Cw&start=200', header=0)
for df in page:
print(df)`
最佳答案
你要找的表有一个class属性gf-table
,传给pd.read_html
,那么它只会读取第二个表:
page = pd.read_html('https://www.google.com/finance/historical?q=a&startdate=Jan%201%2C%202000&enddate=Feb%2028%2C%202017&num=200&ei=_nm3WKGHCIf7jAG74ar4Cw&start=200',
attrs = {'class': 'gf-table'},
header=0)
page
# Date Open High Low Close Volume
#0 May 12, 2016 42.59 42.94 42.42 42.73 2224506
#1 May 11, 2016 42.19 43.20 42.12 42.46 3325515
#2 May 10, 2016 41.50 42.00 41.35 42.00 2094305
#3 May 9, 2016 41.51 41.78 41.29 41.33 1741539
#4 May 6, 2016 40.86 41.62 40.72 41.43 1403476
#5 May 5, 2016 40.64 41.03 40.51 40.96 1083956
#...
关于python - 如何在 python 中使用 pandas 仅从具有两个数据框的网页中选择第二个数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42546686/