Children & Parents attributes of BeautifulSoup

BeautifulSoup Basics
We can extract the parent tags or child tags by using children and parents attributes. To understand this let us create a string with structured parent and child tags.
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
-------

Collecting the Parent Tags

By using parents method we can get all the parent tags. This will return one level up till it reaches the final node. To our above code we will add one more tag <body> and apply the parents method.
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>

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer