使用python特定列的Web刮表

2024-10-04 03:17:54 发布

您现在位置:Python中文网/ 问答频道 /正文

使用python特定列的Web刮表

import pandas as pd

url = 'https://www.sharesansar.com/today-share-price'

tables = pd.read_html(url)
print(tables[1][1])

Tags: httpsimportcomweburlsharepandasread
1条回答
网友
1楼 · 发布于 2024-10-04 03:17:54

正在发生的事情:

Pandas模块“read_html(url)”返回网页中所有要刮取的表的列表。在当前页面中('https://www.sharesansar.com/today-share-price)桌子上只有一个。因此,如果您想访问该表,您应该获取列表的第一个索引

解决方案:

import pandas as pd

url = 'https://www.sharesansar.com/today-share-price'

tables = pd.read_html(url)
df = tables[0]
firstRow = df.iloc[[0]]
firstTwoColumns = df.iloc[: , [0, 1]]

要选择特定的列/行,请使用pandas模块

.iloc

.loc

相关问题 更多 >