【发布时间】:2022-01-16 17:49:19
【问题描述】:
如何使用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']}
【问题讨论】:
-
{'class': ['td', 'p1']}和{'class': ['td p1']}都有效。你到底有什么问题? -
如何使用soup.find找到
<h1 class = 'td p1'> title that i want </h1> -
因为当我做 soup.find_all (class= 'td') 时。 Beautifusoup 发现:
<h1 class = 'td p1'> title that i want </h1> <h1 class = 'td p2'> title that i don't want </h1>当我执行 soup.find_all (class= 'p1') 时。 Beautifusoup 发现:<h1 class = 'td p1'> title that i want </h1> <h1 class = 'p1'> title that i don't want </h1> -
请看下面的答案。是你要找的吗?
-
我放了一个更通用的案例。但我的印象是,就我个人而言,它是有效的。我不知道我们可以放置空格。所以是的,它有效。也许我的问题来自其他地方。所以我将关闭问题
标签: python web-scraping beautifulsoup