Python - 爬虫利器 - BeautifulSoup4常用 API

[复制链接]
发表于 2025-10-28 16:08:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
媒介

   在韶光的长河里,每一滴水都是昨日的星辰,映照着永不重复的本日。
  BeautifulSoup4 简介

BeautifulSoup4(通常简称为 BS4)是一个用于分析 HTML 和 XML 文档的 Python 库。它的计划目的是简化从复杂网页中提取数据的过程。BeautifulSoup4 可以处理处罚各种各样的标志语言,并提供了一个简朴的接口来举行文档导航、搜索和修改。
重要特点:



  • 跨平台支持: Beautiful Soup 支持 Windows、Linux、Mac OS X 等多个操纵体系。
  • 兼容性强: 支持多种分析器,包罗 Python 内置的尺度库分析器 (html.parser)、第三方分析器 lxml 和 html5lib。
  • 易于学习: 提供了简朴且直观的 API,得当初学者利用。
  • 强大功能: 包罗丰富的函数和方法,可以资助开辟者高效地完成任务。
安装方式:

你可以通过 pip 工具轻松安装 BeautifulSoup4:
  1. pip install beautifulsoup4
复制代码
常用 API

以下是 BeautifulSoup4 中一些常用的 API 方法和功能:
1. 创建 BeautifulSoup 对象

起首,你必要创建一个 BeautifulSoup 对象来分析 HTML 或 XML 文档。
  1. from bs4 import BeautifulSoup
  2. # 使用默认的 html.parser 解析器
  3. html_doc = "<html><head><title>Example Page</title></head><body id='id'><a href='123'></a><p class='my-class child-class'><i>444</i><h1>Hello World</h1></p></body></html>"
  4. soup = BeautifulSoup(html_doc, 'html.parser')
  5. # 打印解析后的结果
  6. print(soup.prettify())
复制代码
2. 查找标签

可以通过标署名称或其他属性来查找特定的元素。
find(): 返回匹配的第一个元素

  1. first_paragraph = soup.find('p')
  2. print(first_paragraph)  # 输出: <p>Hello World</p>
复制代码
find_all(): 返回全部匹配的元素列表

  1. all_headings = soup.find_all(['h1', 'h2'])
  2. for heading in all_headings:
  3.     print(heading.text)
复制代码
select_one() & select(): CSS 选择器

  1. css_selector_example = soup.select_one('.my-class')
  2. print(css_selector_example)
  3. css_selectors_examples = soup.select('#id > .child-class')
  4. for element in css_selectors_examples:
  5.     print(element.text)
复制代码
3. 访问标签内容

访问标签内的文本和其他属性。
text 属性: 获取标签内纯文本

  1. text_content = first_paragraph.text
  2. print(text_content)  # 输出: Hello World
复制代码
get_text(): 同样作用于获取文本

  1. get_text_content = first_paragraph.get_text()
  2. print(get_text_content)  # 输出: Hello World
复制代码
attrs 属性: 获取标签的全部属性

  1. attributes = first_paragraph.attrs
  2. print(attributes)  # 如果没有其他属性,则为空字典 {}
复制代码
[attribute]: 直接访问某个属性值

  1. link_tag = soup.a
  2. href_value = link_tag['href']
  3. print(href_value)
复制代码
4. 修改文档

除了查询外,还可以动态地添加、删除或修改文档中的节点。
添加新标签

  1. new_tag = soup.new_tag("b")
  2. new_tag.string = "Bold Text"
  3. first_paragraph.append(new_tag)
  4. print(first_paragraph)  # 输出: <p>Hello World<b>Bold Text</b></p>
复制代码
删除标签

  1. tag_to_remove = soup.b
  2. tag_to_remove.decompose()
  3. print(first_paragraph)  # 输出: <p>Hello World</p>
复制代码
更换标签

  1. replacement_tag = soup.new_tag("i")
  2. replacement_tag.string = "Italic Text"
  3. first_paragraph.i.replace_with(replacement_tag)
  4. print(first_paragraph)  # 输出: <p>Hello World<i>Italic Text</i></p>
复制代码
5. 导航树结构

BeautifulSoup 还提供了多种方法来遍历和操纵 DOM 树。
parent: 上级父节点

  1. parent_node = first_paragraph.parent
  2. print(parent_node.name)  # 输出: body
复制代码
children: 下级子节点迭代器

  1. children_nodes = list(first_paragraph.children)
  2. for child in children_nodes:
  3.     print(child)
复制代码
siblings: 并列兄弟节点

  1. next_sibling = first_paragraph.next_sibling
  2. previous_sibling = first_paragraph.previous_sibling
  3. print(next_sibling)
  4. print(previous_sibling)
复制代码
实战小本领(关键点)

实际环境下,很多节点不好找到,可以利用欣赏器功能,可以直接复制css选择器
F12打开控制台


复制对应图片的css选择器


直接代码中利用

  1. from bs4 import BeautifulSoup
  2. # 使用默认的 html.parser 解析器
  3. html_doc = "<html></html>"
  4. soup = BeautifulSoup(html_doc, 'html.parser')
  5. # 只是为了示例  不可运行 以下是复制出来的内容
  6. soup.select('#ice-container > div.tbpc-layout > div.screen-outer.clearfix > div.main > div.core.J_Core > div > div:nth-child(1) > div:nth-child(1) > div > div > div > div > div:nth-child(3) > div > div > a')
复制代码
竣事语

   文章中API都验证过,可直接运行👽👽👽
运行有题目可接洽作者品评交换🤭🤭🤭
风是自由的,你也是自由🤠🤠🤠
欢迎一起交换学习☠️☠️☠️
有资助请留下足迹 一键三连🥰🥰🥰
爬虫大佬勿喷,欢迎指正题目😈😈😈
反面会做一系列的爬虫文章,请连续关注作者🤡🤡🤡。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表