使用 BeautifulSoup 在 HTML 文档中查找具有给定属性值的标签
从 HTML 页面中提取数据是网页抓取过程中的典型活动。HTML 页面中的许多标签和特性有助于定位和提取相关数据。可以使用一个名为 BeautifulSoup 的著名 Python 模块来解析 HTML 文本并提取有用信息。在本教程中,我们将专注于使用 BeautifulSoup 来定位具有特定属性值的标签。
安装和设置
首先,我们必须安装 BeautifulSoup。可以使用 Python 的包安装程序 Pip 来完成此操作。以下命令应输入到命令窗口或终端中 -
pip install beautifulsoup4
安装后,我们可以使用以下语句在 Python 代码中导入 BeautifulSoup -
from bs4 import BeautifulSoup
语法
使用 BeautifulSoup 查找具有给定属性值的标签的语法如下 -
soup.find(tag_name, attrs={attribute_name: attribute_value})
这里,soup 指的是包含解析的 HTML 内容的 BeautifulSoup 对象,标签名称指的是我们要查找的标签,属性名称指的是我们要查找的属性,属性值与我们要匹配的值相匹配。
算法
使用 BeautifulSoup 解析 HTML 文档
使用 find() 方法查找具有给定属性值的标签
从标签中提取所需数据
示例 1
要查找类为 "important" 的段落标签,我们可以使用以下代码 -
from bs4 import BeautifulSoup html_doc="""<html> <body> <p class="important">Fancy content here, just a test</p> <p>This is a normal paragraph</p> </body> </html>""" soup = BeautifulSoup(html_doc, 'html.parser') tag = soup.find('p', attrs={'class': 'important'}) print(tag)
输出
<p class="important">Fancy content here, just a test</p>
soup 是包含已解析 HTML 文档的 BeautifulSoup 对象,'p' 是我们要查找的标签名称,'class' 是我们要搜索的属性名称,'important' 是我们要匹配的属性值。find() 方法返回符合给定条件的第一个标签,在本例中,返回类为 "important" 的第一个段落标签。
示例 2
要查找 id 为 "content" 的 div 标签内的第一个段落标签,我们可以使用以下代码 -
from bs4 import BeautifulSoup html_doc = """<html> <body> <div id="header"> <h1>Welcome to my website</h1> <p>All the help text needed will be in this paragraph</p> </div> <div id="content"> <h2>Section 1</h2> <p>Content of section 1 goes here</p> <h2>Section 2</h2> <p>Content of section 2 goes here</p> </div> </body> </html> """ soup = BeautifulSoup(html_doc, 'html.parser') div_tag = soup.find('div', attrs={'id': 'content'}) tag = div_tag.find('p') print(tag)
输出
<p>Content of section 1 goes here</p>
这里,soup 是包含已解析 HTML 文档的 BeautifulSoup 对象,'div' 是我们要查找的标签名称,'id' 是我们要搜索的属性名称,'content' 是我们要匹配的属性值。find() 方法返回符合给定条件的第一个 div 标签,在本例中,返回 id 为 "content" 的 div 标签。
示例 3
from bs4 import BeautifulSoup html_doc="""<html> <body> <h1>List of Books</h1> <table> <tr> <th>Title</th> <th>Author</th> <th>Price</th> </tr> <tr> <td><a href="book1.html">Book 1</a></td> <td>Author 1</td> <td>$10</td> </tr> <tr> <td><a href="book2.html">Book 2</a></td> <td>Author 2</td> <td>$15</td> </tr> <tr> <td><a href="book3.html">Book 3</a></td> <td>Author 3</td> <td>$20</td> </tr> </table> </body> </html> """ soup = BeautifulSoup(html_doc, 'html.parser') price_tag = soup.find('td', text='$15') book_tag = price_tag.find_previous('td').find_previous('td').find_previous('td') title = book_tag.text author = book_tag.find_next('td').text print(title, author)
输出
$10 Book 2
此处,"soup"是指具有解析的 HTML 内容的 BeautifulSoup 对象,"td" 代表我们要查找的标签名称,"text" 代表我们要匹配的文本,"$15" 代表该文本的值。在此示例中,find() 函数返回符合指定条件的第一个 td 标签,即带有字符串"$15"的 td 标签。
然后使用 find previous() 函数定位具有书名和 href 属性的 td 元素。使用位于包含值"$15"的 td 标签之前的 td 标签,此方法在文档树中向后查找符合指定条件的第一个标签。
由于我们有书名标签,因此我们可以使用 text 属性来检索文本。下一步是使用 find next sibling() 函数找到包含作者姓名的后续 td 标签。该方法返回书名 td 标签后面的 td 标签,因为它是具有相同父标签的下一个兄弟标签。
应用
一种典型的 Web 抓取活动可用于各种应用,即查找具有特定属性值的标签。
使用网站数据创建机器学习模型或进行数据分析
电子商务网站抓取产品信息和价格比较
使用求职门户网站抓取来分析和跟踪招聘信息
可以使用多种 Web 抓取技术、Python 和 BeautifulSoup 等编程语言以及其他工具来完成此任务。在进行任何在线抓取之前,阅读网站的服务条款是必不可少的,因为有些网站可能已经采取了安全措施来阻止它。
结论
本文介绍了 BeautifulSoup 的设置和安装,BeautifulSoup 是一个强大的 Python 模块,可以从 HTML 和 XML 文档中提取信息,本文介绍了使用给定属性值识别某些标签的语法,并提供了有关如何在实际情况中正确使用这些技术的详细说明。介绍了
find()
和find_all()
方法,以及如何在 HTML 页面中查找具有特定属性值的标签。BeautifulSoup 彻底改变了在线抓取的世界,这是一个灵活而强大的工具,为进一步调查和实验提供了大量空间。