You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用bs4从HTML中提取class和id属性?解决KeyError问题

解决提取HTML属性时的KeyError问题

问题原因

你提供的两段<p>标签中,第二个标签没有id属性,取而代之的是data-id属性。直接通过pr['id']访问不存在的属性会触发KeyError。

修复方案

方案1:使用get()方法安全获取属性

BeautifulSoup的Tag对象支持用get()方法获取属性,当属性不存在时会返回默认值(比如None),避免报错:

from bs4 import BeautifulSoup

html = '''<p transform="translate(3,15)" class="SoccerPlayer SoccerPlayer-1 Soccer-Team  Outcome-Positive" id="12-8-3">
<p transform="translate(89,20)" class="SoccerPlayer SoccerPlayer-514 Soccer-Team Outcome-Positive" data-id="12-9-229">'''

soup = BeautifulSoup(html, 'html.parser')
for pr in soup.find_all("p"):
    class_attr = pr['class']
    id_attr = pr.get('id')
    data_id_attr = pr.get('data-id')
    print(f"class: {class_attr}, id: {id_attr}, data-id: {data_id_attr}")

方案2:先检查属性是否存在再访问

通过in操作符判断属性是否存在,再进行访问:

from bs4 import BeautifulSoup

html = '''<p transform="translate(3,15)" class="SoccerPlayer SoccerPlayer-1 Soccer-Team  Outcome-Positive" id="12-8-3">
<p transform="translate(89,20)" class="SoccerPlayer SoccerPlayer-514 Soccer-Team Outcome-Positive" data-id="12-9-229">'''

soup = BeautifulSoup(html, 'html.parser')
for pr in soup.find_all("p"):
    class_attr = pr['class']
    id_attr = pr['id'] if 'id' in pr.attrs else None
    data_id_attr = pr['data-id'] if 'data-id' in pr.attrs else None
    print(f"class: {class_attr}, id: {id_attr}, data-id: {data_id_attr}")

补充说明

如果需要提取所有标识性ID(包括id和data-id),可以合并处理:

from bs4 import BeautifulSoup

html = '''<p transform="translate(3,15)" class="SoccerPlayer SoccerPlayer-1 Soccer-Team  Outcome-Positive" id="12-8-3">
<p transform="translate(89,20)" class="SoccerPlayer SoccerPlayer-514 Soccer-Team Outcome-Positive" data-id="12-9-229">'''

soup = BeautifulSoup(html, 'html.parser')
for pr in soup.find_all("p"):
    class_attr = pr['class']
    player_id = pr.get('id') or pr.get('data-id')
    print(f"class: {class_attr}, player_id: {player_id}")

内容的提问来源于stack exchange,提问作者JayRSP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 10:20:22