如何在Python中使用 Beautifulsoup 抓取评论?
评论由 Beautiful Soup 提供,它是Python的网络抓取框架。网络抓取是使用自动化工具从网站中提取数据的过程,以加快过程。 Comment 对象只是一种特殊类型的 NavigableString,用于使代码库更具可读性。
句法:
下面给出的例子解释了 Beautiful Soup 中注释的概念。
示例 1:在本示例中,我们将创建一条评论。
Python3
# Import Beautiful Soup
from bs4 import BeautifulSoup
# Create the document
doc = ""
# Initialize the object with the document
soup = BeautifulSoup(doc, "html.parser")
# Get the whole comment inside b tag
comment = soup.b
# Print the comment
print(comment)
Python3
# Import Beautiful Soup
from bs4 import BeautifulSoup
# Create the document
doc = ""
# Initialize the object with the document
soup = BeautifulSoup(doc, "html.parser")
# Get the whole comment inside b tag
comment = soup.b.string
# Print the type of the comment
print(type(comment))
输出:
示例 2:在本示例中,我们将创建一个评论并查看其类型。
蟒蛇3
# Import Beautiful Soup
from bs4 import BeautifulSoup
# Create the document
doc = ""
# Initialize the object with the document
soup = BeautifulSoup(doc, "html.parser")
# Get the whole comment inside b tag
comment = soup.b.string
# Print the type of the comment
print(type(comment))
输出: