目录列表Python Beautifulsoup
内容列表由 Beautiful Soup 提供,它是Python的网络抓取框架。网络抓取是使用自动化工具从网站中提取数据的过程,以加快过程。内容是一个包含标签子元素的列表。
句法:
tag.contents
下面给出的例子解释了 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 whole content from the body tag
contents = soup.body.contents
# Print the contents
print(contents)
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 whole content from the body tag
contents = soup.body.contents
# Print the type of contents
print(type(contents))
输出:
[ 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 whole content from the body tag
contents = soup.body.contents
# Print the type of contents
print(type(contents))
输出: