首页 > 解决方案 > 如何忽略beautifulsoup4 python上的标签

问题描述

我正在做一个新项目,但我遇到了一些问题。

我的问题就是这样。

<div class="news">
      <p class="breaking">  </p>
      ...
<p> i need to pull here. </p>

但是 class = "break" 是不允许我这样做的。我想忽略“破坏”类并拉动<p>.

标签: pythonpython-3.xbeautifulsouphtml-parser

解决方案


也许,class=''会做find_allor findAll

from bs4 import BeautifulSoup

html = """
<div class="news">
      <p class="breaking">  </p>
      ...
<p> i need to pull here. </p>

"""

soup = BeautifulSoup(html, 'html.parser')

print(soup.find_all('p', class_=''))
print(soup.findAll(True, {'class': ''}))

输出

[<p> i need to pull here. </p>]
[<p> i need to pull here. </p>]

推荐阅读