Python 通过pandas read#u HTML won'获取HTML表格;行不通 什么有效
我设法通过Python 通过pandas read#u HTML won'获取HTML表格;行不通 什么有效,python,pandas,dataframe,beautifulsoup,Python,Pandas,Dataframe,Beautifulsoup,我设法通过pd从hmtl表中获取数据 [1]中的 将numpy作为np导入 作为pd进口熊猫 从表格导入表格 URL=”https://coinmarketcap.com/all/views/all/" df_in_list=pd.read_html(URL,attrs={'id':'currencesall'}) #_列表中的df_具有元素0中的df df_raw=df_在_列表中[0] df=df_在_列表中[0] df=df[['#','Name','Symbol','Market Cap
pd从hmtl表中获取数据
[1]中的
将numpy作为np导入
作为pd进口熊猫
从表格导入表格
URL=”https://coinmarketcap.com/all/views/all/"
df_in_list=pd.read_html(URL,attrs={'id':'currencesall'})
#_列表中的df_具有元素0中的df
df_raw=df_在_列表中[0]
df=df_在_列表中[0]
df=df[['#','Name','Symbol','Market Cap','Price']]
打印(制表(df.head(),headers='keys',tablefmt='psql'))
通过Chrome开发工具找到div id:
是的,表的类不正确
如果您将df_in_list
更改为df_in_list=pd.read_html(URL,attrs={'class':'table'})
它应该可以工作
您还必须更改df=df[['#','Name','Symbol','Market Cap','Price']]
部分,因为这些列不在您要删除的新表中。太棒了!非常感谢。
Out[1]:
+----+-----+------------------+----------+-----------------+-----------+
| | # | Name | Symbol | Market Cap | Price |
|----+-----+------------------+----------+-----------------+-----------|
| 0 | 1 | BTC Bitcoin | BTC | $95,224,161,781 | $5398.69 |
| 1 | 2 | ETH Ethereum | ETH | $19,256,205,102 | $182.34 |
| 2 | 3 | XRP XRP | XRP | $15,031,762,618 | $0.359679 |
| 3 | 4 | LTC Litecoin | LTC | $5,530,275,811 | $90.24 |
| 4 | 5 | BCH Bitcoin Cash | BCH | $5,514,209,793 | $311.17 |
+----+-----+------------------+----------+-----------------+-----------+
<div id="historical-data" class="tab-pane active">
Out[2]:
ValueError: No tables found
<div id="historical-data" class="tab-pane active">