如何使用Beautiful Soup拉没有属性的<p>标签?

Mik*_*ler 1 python beautifulsoup

说一个网页包含以下内容:

<p style="display: none;"><input id="ak_js" name="ak_js" type="hidden" value="68"/></p>

<p><b>Lack of sales.. ANY sales.</b></p>
Run Code Online (Sandbox Code Playgroud)

我正在尝试编写仅拉第二个标签的代码。基本上所有不包含属性的段落标签。我在下面尝试了以下两段代码,但它们没有给我想要的结果。

text = BeautifulSoup(requests.get(url).text)

for tag in text.find_all("p", attrs = False):
    .....

for tag in text.find_all(re.compile("^<p>$")):
    ....
Run Code Online (Sandbox Code Playgroud)

解决此问题的最佳方法是什么?

Cyr*_*bil 5

您可以给一个lambda find_all并对其进行过滤。

soup.find_all(lambda tag: tag.name == 'p' and not tag.attrs)
Run Code Online (Sandbox Code Playgroud)