mkHun
mkHun

Reputation: 5927

Beautifulsoup find the tag and attribute of without value?

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

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

So I have tried the following one

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

I have tried below one also

bread = contSoup.find("nav breadcrumbs")

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

Upvotes: 2

Views: 2484

Answers (1)

Keyur Potdar
Keyur Potdar

Reputation: 7238

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

Output:

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

Upvotes: 4

Related Questions