Beautifulsoup找到没有价值的标签和属性? [英] Beautifulsoup find the tag and attribute of without value?

查看:80
本文介绍了Beautifulsoup找到没有价值的标签和属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取具有属性但没有值的特定标签的内容.例如,如何获取它

I'm trying to get the content of the particular tag which having the attribute but no values. How can I get it for example

cont = '<nav></nav> <nav breadcrumbs> <a href="">aa</a></nav> <nav></nav>'

从上面的内容中我要提取<nav breadcrumbs> <a href="">aa</a></nav>

From the above one I want to extract the <nav breadcrumbs> <a href="">aa</a></nav>

所以我尝试了以下一个

bread = contSoup.find("nav",{"breadcrumbs":""})

我也尝试过以下一个

bread = contSoup.find("nav breadcrumbs")

最后我正在使用RegEx来获取此数据,我能够得到答案,但是如何从美丽的汤中做

Finally I'm using RegEx to get this data, I'm able to get the answer, but how can I do it from the beautiful soup

推荐答案

在这种情况下,您可以使用attr=True.

You can use attr=True for this case.

cont = '<nav></nav> <nav breadcrumbs> <a href="">aa</a></nav> <nav></nav>'
soup = BeautifulSoup(cont, 'lxml')  # works with 'html.parser' too.
print(soup.find('nav', breadcrumbs=True))
# which is the same as print(soup.find('nav', {'breadcrumbs': True}))

输出:

<nav breadcrumbs=""> <a href="">aa</a></nav>

这篇关于Beautifulsoup找到没有价值的标签和属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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