我试图做一个网络抓取,但我的熊猫DF似乎是一个清单,所以我不能出口到卓越。
如何从这个列表中删除df?
from selenium import webdriver
import pandas as pd
link = 'https://www.reit.com/data-research/reit-market-data/us-reit-industry-equity-market-cap'
driver = webdriver.Chrome(options=options)
driver.get(link)
df = pd.read_html(driver.page_source)
print(df)它还我
[ End of Year All REITs # of REITs Market Capitalization Equity # of REITs Market Capitalization.1 Mortgage # of REITs Market Capitalization.2 Hybrid # of REITs Market Capitalization.3
0 1971 34 1494.3 12 332.0 12 570.8 10 591.6
1 1972 46 1880.9 17 377.3 18 774.7 11 728.9
2 1973 53 1393.5 20 336.0 22 517.3 11 540.2
3 1974 53 712.4 19 241.9 22 238.8 12 231.7
4 1975 46 899.7 12 275.7 22 312.0 12 312.0
5 1976 62 1308.0 27 409.6 22 415.6 13 482.8
6 1977 69 1528.1 32 538.1 19 398.3 18 591.6
7 1978 71 1412.4 33 30 2001 182 154898.6 151 147092.1 22 3990.5 9 3816.0
42 2013 202 670334.1 161 608276.6 41 62057.4 -- --
43 2014 216 907425.5 177 846410.3 39 61017.2 -- --
44 2015 233 938852.0 182 886487.5 41 52364.6 -- --
45 2016 224 1018729.9 184 960192.8 40 58537.1 -- --
46 2017 222 1133697.6 181 1065947.7 41 67749.9 -- --
47 2018 226 1047641.3 186 980314.9 40 67326.4 -- --
48 2019 219 1328806.2 179 1245878.3 40 82927.8 -- --
49 2020 223 1249186.3 182 1184150.2 41 65036.1 -- --]
Traceback (most recent call last):
File "...", line 31, in <module>
df.to_excel(...., index=False)
AttributeError: 'list' object has no attribute 'to_excel'
Process finished with exit code 1发布于 2021-05-29 16:16:42
pd.read_html总是返回数据的列表,因为一个网页中可能有多个<table>元素。因为您需要在可能的1元素列表中包含元素,所以您可以这样做。
# note the comma!
df, = pd.read_html(driver.page_source)或者更易读:
df = pd.read_html(driver.page_source)[0]然后,您可以像往常一样继续,例如,df.to_excel(...)。
https://stackoverflow.com/questions/67753449
复制相似问题