使用 BeautifulSoap 查找 HTML 标签
原文:https://www.studytonight.com/python/web-scraping/find-tags-with-beautifulsoup
在本教程中,我们将学习如何使用美化输出模块搜索任何标签。我们建议您浏览之前的教程,这些教程是关于美丽的耦合模块的基本介绍,以及涵盖美丽的耦合模块的所有有用方法的教程。
我们已经学习了不同的遍历 HTML 树的方法,如parent
、parents
、next_sibling
、previous_sibling
等。但是使用这些方法很难找到所有相似的标签。所以,现在我们将学习如何使用“美丽输出”模块的find
和find_all
方法来找到任何标准的 HTML 标签。
如果你来自上一个教程,我们将使用相同的 html 代码,如果你是新来的,请创建一个文件sample _ 网页. html 并在其中复制以下 HTML 代码:
<!DOCTYPE html>
<html>
<head>
<title> Sample HTML Page</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
width: 95%;
height: 75px;
margin: 10px 2.5%;
border: 1px dotted grey;
text-align: center;
}
p {
font-family: sans-serif;
font-size: 18px;
color: #000;
line-height: 75px;
}
a {
position: relative;
top: 25px;
}
</style>
</head>
<body>
<div id="first-div">
<p class="first">First Paragraph</p>
</div>
<div id="second-div">
<p class="second">Second Paragraph</p>
</div>
<div id="third-div">
<a href="https://www.studytonight.com">Studytonight</a>
<p class="third">Third Paragraph</p>
</div>
<div id="fourth-div">
<p class="fourth">Fourth Paragraph</p>
</div>
<div id="fifth-div">
<p class="fifth">Fifth Paragraph</p>
</div>
</body>
</html>
要读取上述 HTML 文件的内容,请使用以下 python 代码将内容存储到变量中:
## reading content from the file
with open("sample_webpage.html") as html_file:
html = html_file.read()
一旦我们读取了文件,我们就创建了一个美丽的输出对象:
import bs4
## reading content from the file
with open("sample_webpage.html") as html_file:
html = html_file.read()
## creating a BeautifulSoup object
soup = bs4.BeautifulSoup(html, "html.parser")
刮网过程开始了...
美丽组:find_all
方法
find_all
方法用于通过将标签的名称作为方法的参数来查找我们正在搜索的所有相似标签。find_all
方法返回一个包含所有找到的 HTML 元素的列表。以下是语法:
find_all(name, attrs, recursive, limit, **kwargs)
我们将逐一介绍find_all
方法的所有参数。让我们从名称参数开始。
find_all
:名称参数
让我们从 HTML 代码中找到所有的p
标签:
import bs4
## reading content from the file
with open("sample_webpage.html") as html_file:
html = html_file.read()
## creating a BeautifulSoup object
soup = bs4.BeautifulSoup(html, "html.parser")
## finding all p tags
p_tags = soup.find_all("p")
print(p_tags)
print("\n-----Class Names Of All Paragraphs-----\n")
for tag in p_tags:
print(tag['class'][0])
print("\n-----Content Of All Paragraphs-----\n")
for tag in p_tags:
print(tag.text)
【
第一段
、
第二段
、
第三段
、
第四段
、
第五段
——所有段落的类别名称——第一、二、三、四、五——所有段落的内容——第一段第二段第三段第四段第五段
如您所见,我们不仅可以找到标签,还可以找到与这些标签相关的所有信息。
find_all
:属性参数
让我们从 HTML 代码中找到所有具有属性 class
等于链接的标签(该代码是在我们在上面的代码片段中创建了soup
对象之后):
## finding using class name
link_class_tags = soup.find_all(class_="link")
print(link_class_tags)
print("----------")
for tag in link_class_tags:
print(tag.name)
谷歌 - a
请注意为class
属性提供下划线(_
)的语法,您必须遵循该语法。
find_all
:包含任意字符串的标签
我们可以使用find_all
方法找到包含给定字符串的所有 HTML 标签。由于方法find_all
需要搜索一个正则表达式,因此在下面的代码示例中,我们使用了 python 的re
模块来生成一个正则表达式。
## finding tags using a string
## importing regular expression module to find all the strings
import re
## defining an re variable which contains "Paragraph" text
s = re.compile("Paragraph")
## finding all the content of the tags which contains "Paragraph"
tags_containing_paragraph = soup.find_all(string=s)
print(tags_containing_paragraph)
[“第一段”、“第二段”、“第三段”、“第四段”、“第五段”]
编写上述代码时,将import re
语句和import bs4
语句保留在顶部。
find_all
:极限参数
限制参数用于限制结果集。当提供了一个限制时find_all
方法只返回等于给定限制的标签,其他符合条件的标签不包括在返回的列表中。
## finding the first p tag using limit parameter
first_p_tag = soup.find_all("p", limit=1)
print(first_p_tag)
第一段
您可以像本例中一样一起使用多个参数。
美丽组:find
方法
find
方法用于寻找第一个匹配标签。这类似于将limit=1
参数值传递给find_all
方法。
让我们举个例子:
p_tag = soup.find("p")
print(p_tag)
print("----------")
print(p_tag.text)
第一段
-第一段
再举一个例子,
a_tag = soup.find("a")
print(a_tag)
print("----------")
print(a_tag.text)
print("\n")
print(a_tag['href'])
StudyTonight -StudyTonight https://www.studytonight.com
在这之后,我们学习了使用美丽的输出模块进行网页抓取。我们已经涵盖了所有重要和有用的方法,但还有很多。如果您想深入了解,请查看美观组文档。
在下一个教程中,我们将刮一个网站。