我如何拉< p>标签没有属性使用美丽汤? [英] How do I pull <p> tags without attributes using Beautiful Soup?

查看:47
本文介绍了我如何拉< p>标签没有属性使用美丽汤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Say a web page contains the following:

<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>

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

I'm trying to write code that would pull only the second tag. Basically all paragraph tags that don't contain attributes. I tried the following two pieces of code below, but they don't get me the results I want.

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

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

for tag in text.find_all(re.compile("^<p>$")):
    ....

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

What's the best way to solve this?

推荐答案

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

You can give a lambda to find_all and filter with it.

soup.find_all(lambda tag: tag.name == 'p' and not tag.attrs)

这篇关于我如何拉&lt; p&gt;标签没有属性使用美丽汤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆