Beautifulsoup find the tag and attribute of without value?

前端 未结 1 1677
南方客
南方客 2021-01-27 04:34

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


        
相关标签:
1条回答
  • 2021-01-27 05:06

    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>
    
    0 讨论(0)
提交回复
热议问题