Question

I have html code like this,

<div style="width:200px">
    <h2> My name1 </h2>
     DOB:17-6-1991
    <br>
    person details, person details,person details
    <div></div>
    <h2> My name2</h2>
     DOB:18-6-1991
    <br>
    person details, person details,person details
    <div></div>
    <h2> My name3 </h2>
     DOB:19-6-1991
    <br>
    person details, person details,person details
    <div></div>
    <h2> My name4 </h2>
     DOB:20-6-1991
    <br>
    person details, person details,person details
    <div></div>
    <h2> My name5 </h2>
     DOB:21-6-1991
    <br>
    person details, person details,person details
    <div></div>
</div>        

I am using python BeautifulSoup for parsing the html code. In the above code i want contents like this,

My name1
17-6-1991
person details, person details,person details

My name2
18-6-1991
person details, person details,person details
.
.
.
.
so on

Please help me to solve this problem

Was it helpful?

Solution

There are many ways to solve your problem. I chose to iterate over the h2 elements in a loop, then over their siblings in another loop. I break out of the inner loop, when I encounter another h2. I did not remove whitespace. You can do that with Python methods such as rtrim and ltrim. You can get rid of the "DOB:" with string.replace.

from bs4 import BeautifulSoup
from bs4 import NavigableString

s = """your HTML here"""

soup = BeautifulSoup(s)
headers = soup.find_all("h2")
for h in headers:
   print h.text
   for s in h.next_siblings:
      if s.name == "h2":
         break
      elif isinstance(s, NavigableString):
         print s.string
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top