Python Forum
How to get the first child of a beautifulSoup document ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the first child of a beautifulSoup document ?
#3
The doc about this .contents and .children.

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'
Reply


Messages In This Thread
RE: How to get the first child of a beautifulSoup document ? - by snippsat - Jun-08-2020, 06:21 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020