content = """<h2>List of web programming languages</h2>
<UL>
<li>PHP</li>
<li>Python</li>
<li>JavaScript</li>
</UL>"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(content, 'html.parser')
Now our soup is ready, we will use this to get child nodes. By using this method we will get a list and we can use for loop to iterate through all the elements.
for child in soup.ul.children:
print(child)
print(child.string)
Output is here ( part only )
<li>PHP</li>
PHP
<li>Python</li>
Python
-------
content = """<body><h2>List of web programming languages</h2>
<UL>
<li>PHP</li>
<li>Python</li>
<li>JavaScript</li>
</UL></body>"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(content, 'html.parser')
for parent in soup.li.parents:
print(parent)
Output is here
<ul>
<li>PHP</li>
<li>Python</li>
<li>JavaScript</li>
</ul>
<body><h2>List of web programming languages</h2>
<ul>
<li>PHP</li>
<li>Python</li>
<li>JavaScript</li>
</ul></body>
<body><h2>List of web programming languages</h2>
<ul>
<li>PHP</li>
<li>Python</li>
<li>JavaScript</li>
</ul></body>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.