问题
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
回答1:
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>
来源:https://stackoverflow.com/questions/49205643/beautifulsoup-find-the-tag-and-attribute-of-without-value