Issue
I have an html that looks like this:
<h3>
<a href="google.com">hellw</a>
</h3>
<ol>
<li>
....
</li>
</ol>
<h3>
<a href= "dddd">dddd</a>
</h3>
<ol>
<li>
<ol>
....
</li>
</ol>
I want to get ol next to h3. I have tried this but it isnt working
soup = BeautifulSoup(html_string)
print(soup.h3.next_element)
This gives out a inside of h3. How do I get all the ols in a list?
Edit:
I have tried this but it only gives me lis within the first ol tags = soup.h3.nextSibling.find_all()
and not all ols.
I need ols in a list so I could iterate it later.
Solution
You can first grab all h3
tags in a list. Once you have this done, just find all ol
tags while iterating through that list. It would look something like this:
html_string = """
...
"""
soup = BeautifulSoup(html_string, 'html.parser')
# all h3 tags
h3_tags = soup.find_all('h3')
# all ol tags
ol_tags = [each_h3.find_next('ol') for each_h3 in h3_tags]
This will give you output of:
[<ol>
<li>
....
</li>
</ol>, <ol>
<li>
<ol>
....
</ol></li>
</ol>]
Answered By - Rustam Garayev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.