1

I have Html structure like

<html>
<body>
    <-- Some tags -->
    <div class="main-sv">
         <div class="first-sv custom-sv">
            <-- Some tags and content-->
         </div>
    </div>    
</body>
</html>

I want to check out if class value of first child of div which class value is main-sv and child tag is div and class value contains First-sv sub-string.

Following in my code which work fine

>>> "Frist-sv" in dict(soup.find("div", {"class" :"main-sv"}).findChild().attrs)["class"].split(" ")
True

still any other way like xpath in lxml ?

I have to use beautifulsoup only

Vivek Sable
  • 9,938
  • 3
  • 40
  • 56

1 Answers1

0

No, as stated here BeautifulSoup itself does not support xpath look-up. But here is a slightly more simplified solution:

from bs4 import BeautifulSoup

html = """
<div class="main-sv">
     <div class="first-sv custom-sv">
        <-- Some tags and content-->
     </div>
</div>
"""

soup = BeautifulSoup(html, 'html.parser')

print 'first-sv' in soup.find('div', {'class':'main-sv'}).find('div')['class']
# prints True

Or another one, using select

parent = soup.find('div', {'class':'main-sv'})
child = parent.select('div')[0]
print 'first-sv' in child['class']
# prints True
Community
  • 1
  • 1
vadimhmyrov
  • 169
  • 7