如何提取特定类名后跟特定文本的文本?

2024-06-12 00:09:40 发布

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

我正在尝试用Python中的BeautifulSoup收集webdata。我特别尝试提取不同类型的汽车特征。例如,在下面粘贴的html代码中,我试图将“远程启动”、“导航系统”和“加热方向盘”提取为“方便”功能。有人能让我知道如何提取和存储每个这样的类别的功能吗?你知道吗

enter image description here


Tags: 代码功能类型远程粘贴html特征类别
1条回答
网友
1楼 · 发布于 2024-06-12 00:09:40

下面是一个方法:

import bs4
your_source_code = "<html>..."
soup = bs4.BeautifulSoup(your_source_code, "html.parser")

result = {}

for group in soup.find_all("div", {"class": "details-feature-list normalized-features"}):
    result[group.find("h2", {"class": "cui-heading-2"}).text] = [itm.text for itm in group.find_all("li", {"class": "details-feature-list__item"})]

结果是这样的:
{"Convenience": ["Remote Start", "Navigation System", "Heated Steering Wheel"]}

相关问题 更多 >