NavigableString 类Python Beautifulsoup
NavigableString类由 Beautiful Soup 提供,它是Python的网络抓取框架。网络抓取是使用自动化工具从网站中提取数据的过程,以加快过程。字符串对应于标签中的一小段文本。 Beautiful Soup 使用 NavigableString 类来包含这些文本位。
句法:
String here
下面给出的例子解释了 Beautiful Soup 中 NavigableString 类的概念。
示例 1:在此示例中,我们将查看字符串的类型。
Python3
# Import Beautiful Soup
from bs4 import BeautifulSoup
# Initialize the object with a HTML page
soup = BeautifulSoup('''
Heading 1
Heading 2
''', "lxml")
# Get the whole h2 tag
tag = soup.h2
# Get the string inside the tag
string = tag.string
# Print the type
print(type(string))
Python3
# Import Beautiful Soup
from bs4 import BeautifulSoup
# Initialize the object with a HTML page
soup = BeautifulSoup('''
Heading 1
Heading 2
''', "lxml")
# Get the whole h2 tag
tag = soup.h2
# Get the string inside the tag and convert
# it into string
string = str(tag.string)
# Print the type
print(type(string))
输出:
示例 2:在本示例中,我们将把 NavigableString 转换为 Unicode字符串。
蟒蛇3
# Import Beautiful Soup
from bs4 import BeautifulSoup
# Initialize the object with a HTML page
soup = BeautifulSoup('''
Heading 1
Heading 2
''', "lxml")
# Get the whole h2 tag
tag = soup.h2
# Get the string inside the tag and convert
# it into string
string = str(tag.string)
# Print the type
print(type(string))
输出: