儿童生成器Python Beautifulsoup
Children生成器由 Beautiful Soup 提供,它是Python的网络抓取框架。网络抓取是使用自动化工具从网站中提取数据的过程,以加快过程。子项生成器用于迭代标签的子项。每个孩子都将成为标签元素。
句法:
tag.children
下面给出的例子解释了 Beautiful Soup 中子生成器的概念。
示例 1:在此示例中,我们将获取元素的子元素。
Python3
# Import Beautiful Soup
from bs4 import BeautifulSoup
# Create the document
doc = " Hello world "
# Initialize the object with the document
soup = BeautifulSoup(doc, "html.parser")
# Get the body tag
tag = soup.body
# Print the children of tag
for child in tag.children:
print(child)
Python3
# Import Beautiful Soup
from bs4 import BeautifulSoup
# Create the document
doc = " Hello world "
# Initialize the object with the document
soup = BeautifulSoup(doc, "html.parser")
# Get the body tag
tag = soup.body
# Print the type of children of tag
for child in tag.children:
print(type(child))
输出:
Hello world
示例 2:在此示例中,我们将查看子项的类型。
蟒蛇3
# Import Beautiful Soup
from bs4 import BeautifulSoup
# Create the document
doc = " Hello world "
# Initialize the object with the document
soup = BeautifulSoup(doc, "html.parser")
# Get the body tag
tag = soup.body
# Print the type of children of tag
for child in tag.children:
print(type(child))
输出: