如何从网页中的表中刮取所有元素?

2024-04-19 22:46:41 发布

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

我在玩下面的代码。我只是想从一个表中获取所有元素,我以为下面的代码可以做到这一点,但我得到的只是一条消息,上面写着:“无”

website_url = requests.get('https://google_cloud_platform.html').text
from bs4 import BeautifulSoup
soup = BeautifulSoup(website_url,'lxml')
print(soup.prettify())

My_table = soup.find('table',{'class':'p6n-table-full-width p6n-space-above-large p6n-table'})
print(My_table)

这是一个项目的形象,我试图拉进来。你知道吗

enter image description here

也许我需要找其他类型的标识符。我不确定到底要找什么。有没有办法列出所有表名?也许它实际上有一个不同的名字,或者ID,或者类似的东西。你知道吗

我开始觉得这甚至不是一张真正的桌子。当我运行下面的脚本时,会得到这样的结果:“IndexError:list index out of range”。这让我觉得网页上连一张表都没有。但是,根据我发布的屏幕截图,有一个叫做“表类”的东西。你知道吗

import pandas as pd
import requests
from bs4 import BeautifulSoup

res = requests.get("https://google_cloud_platform.html")
soup = BeautifulSoup(res.content,'lxml')
table = soup.find_all('table')[0] 
df = pd.read_html(str(table))
print(df[0].to_json(orient='records'))

Tags: 代码httpsimportcloudurlgethtmlgoogle