如何使用BeautifulSoup
查找属性中包含空格的html元素
<h1 class='td p1'>
title that i want
</h1>
<h1 class='td p2'>
title that i don't want
</h1>
<h1 class='p1'>
title that i don't want
</h1>
我想知道如何使用soup.find
找到 title that i want
。
因为beautifulsoup
考虑 title 'that i want'
的属性 attrs像这样:{'class': ['td', 'p1']}.<br>
但不是这样的:{'class': ['td p1']}
最佳答案
注意 不同的方法,但都有共同点来显式选择类。
查找()
soup.find('h1', attrs={'class':'td p1'})
select_one()
soup.select_one('h1.td.p1')
示例
from bs4 import BeautifulSoup
data="""
<h1 class='td p1'>
title that i want
</h1>
<h1 class='td p2'>
title that i don't want
</h1>
<h1 class='p1'>
title that i don't want
</h1>
"""
soup=BeautifulSoup(data,"html.parser")
title = soup.select_one('h1.td.p1')
print(title)
输出
<h1 class="td p1">
title that i want
</h1>
关于python - BeautifulSoup 找到一个属性中包含空格的 html 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70331559/