Jun-08-2020, 06:21 PM
The doc about this .contents and .children.
Here a working test setup.
Find a tag then use children.
One more using CSS selector which is powerful and often forgotten.
Here a working test setup.
from bs4 import BeautifulSoup html = '''\ <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>The HTML5</title> </head> <body> <tag1> <tag2 name="tag2"> <tag3 name="Korea">Closed</tag3><br> <tag3 name="China">A Big contry</tag3><br> <tag3 name="Japan">Nippon</tag3><br> </tag2> </tag1> </body> </html>''' soup = BeautifulSoup(html, 'lxml')Usage test:
>>> for elem in soup.children: ... print(elem) ... html <html lang="en"> <head> <meta charset="utf-8"/> <title>The HTML5</title> </head> <body> <tag1> <tag2 name="tag 2"> <tag3 name="Korea"></tag3>Closed<br/> <tag3 name="China"></tag3>A Big contry<br/> <tag3 name="Japan"></tag3>Nippon<br/> </tag2> </tag1> </body> </html>So it works as expected when use children on soup object.
Find a tag then use children.
>>> tag_1 = soup.find('tag1') >>> for elem in tag_1.children: ... print(elem) ... <tag2 name="tag2"> <tag3 name="Korea">Closed</tag3><br/> <tag3 name="China">A Big contry</tag3><br/> <tag3 name="Japan">Nippon</tag3><br/> </tag2>I don't remember last i used
children
,so it's not much used.One more using CSS selector which is powerful and often forgotten.
>>> find_china = soup.select_one('tag3:nth-child(3)') >>> find_china <tag3 name="China">A Big contry</tag3> >>> find_china.text 'A Big contry'