BeautifulSoup 中的字符串属性 – Python
字符串属性由 Beautiful Soup 提供,它是Python的网络抓取框架。网络抓取是使用自动化工具从网站中提取数据的过程,以加快过程。如果一个标签只有一个孩子,并且该孩子是NavigableString ,则可以使用 访问该孩子。字符串。
先决条件:美丽的汤安装。
句法:
tag.string
下面给出的例子解释了 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 b tag
tag = soup.b
# Print the string
print(tag.string)
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 b tag
tag = soup.b
# Print the type of string
print(type(tag.string))
输出:
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 b tag
tag = soup.b
# Print the type of string
print(type(tag.string))
输出: