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
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.