当前位置: 首页 > news >正文

Python - 爬虫利器 - BeautifulSoup4常用 API

文章目录

    • 前言
    • BeautifulSoup4 简介
      • 主要特点:
      • 安装方式:
    • 常用 API
      • 1. 创建 BeautifulSoup 对象
      • 2. 查找标签
        • find(): 返回匹配的第一个元素
        • find_all(): 返回所有匹配的元素列表
        • select_one() & select(): CSS 选择器
      • 3. 访问标签内容
        • text 属性: 获取标签内纯文本
        • get_text(): 同样作用于获取文本
        • attrs 属性: 获取标签的所有属性
        • [attribute]: 直接访问某个属性值
      • 4. 修改文档
        • 添加新标签
        • 删除标签
        • 替换标签
      • 5. 导航树结构
        • parent: 上级父节点
        • children: 下级子节点迭代器
        • siblings: 并列兄弟节点
    • 实战小技巧(关键点)
      • F12打开控制台
      • 复制对应图片的css选择器
      • 直接代码中使用
    • 结束语

前言

在时光的长河里,每一滴水都是昨日的星辰,映照着永不重复的今天。

BeautifulSoup4 简介

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

主要特点:

  • 跨平台支持: Beautiful Soup 支持 Windows、Linux、Mac OS X 等多个操作系统。
  • 兼容性强: 支持多种解析器,包括 Python 内置的标准库解析器 (html.parser)、第三方解析器 lxmlhtml5lib
  • 易于学习: 提供了简单且直观的 API,适合初学者使用。
  • 强大功能: 包含丰富的函数和方法,可以帮助开发者高效地完成任务。

安装方式:

你可以通过 pip 工具轻松安装 BeautifulSoup4:

pip install beautifulsoup4

常用 API

以下是 BeautifulSoup4 中一些常用的 API 方法和功能:

1. 创建 BeautifulSoup 对象

首先,你需要创建一个 BeautifulSoup 对象来解析 HTML 或 XML 文档。

from bs4 import BeautifulSoup# 使用默认的 html.parser 解析器
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>"
soup = BeautifulSoup(html_doc, 'html.parser')# 打印解析后的结果
print(soup.prettify())

2. 查找标签

可以通过标签名称或其他属性来查找特定的元素。

find(): 返回匹配的第一个元素
first_paragraph = soup.find('p')
print(first_paragraph)  # 输出: <p>Hello World</p>
find_all(): 返回所有匹配的元素列表
all_headings = soup.find_all(['h1', 'h2'])
for heading in all_headings:print(heading.text)
select_one() & select(): CSS 选择器
css_selector_example = soup.select_one('.my-class')
print(css_selector_example)css_selectors_examples = soup.select('#id > .child-class')
for element in css_selectors_examples:print(element.text)

3. 访问标签内容

访问标签内的文本和其他属性。

text 属性: 获取标签内纯文本
text_content = first_paragraph.text
print(text_content)  # 输出: Hello World
get_text(): 同样作用于获取文本
get_text_content = first_paragraph.get_text()
print(get_text_content)  # 输出: Hello World
attrs 属性: 获取标签的所有属性
attributes = first_paragraph.attrs
print(attributes)  # 如果没有其他属性,则为空字典 {}
[attribute]: 直接访问某个属性值
link_tag = soup.a
href_value = link_tag['href']
print(href_value)

4. 修改文档

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

添加新标签
new_tag = soup.new_tag("b")
new_tag.string = "Bold Text"
first_paragraph.append(new_tag)
print(first_paragraph)  # 输出: <p>Hello World<b>Bold Text</b></p>
删除标签
tag_to_remove = soup.b
tag_to_remove.decompose()
print(first_paragraph)  # 输出: <p>Hello World</p>
替换标签
replacement_tag = soup.new_tag("i")
replacement_tag.string = "Italic Text"
first_paragraph.i.replace_with(replacement_tag)
print(first_paragraph)  # 输出: <p>Hello World<i>Italic Text</i></p>

5. 导航树结构

BeautifulSoup 还提供了多种方法来遍历和操作 DOM 树。

parent: 上级父节点
parent_node = first_paragraph.parent
print(parent_node.name)  # 输出: body
children: 下级子节点迭代器
children_nodes = list(first_paragraph.children)
for child in children_nodes:print(child)
siblings: 并列兄弟节点
next_sibling = first_paragraph.next_sibling
previous_sibling = first_paragraph.previous_sibling
print(next_sibling)
print(previous_sibling)

实战小技巧(关键点)

实际情况下,很多节点不好找到,可以利用浏览器功能,可以直接复制css选择器

F12打开控制台

F12打开控制台

复制对应图片的css选择器

复制css选择器

直接代码中使用

from bs4 import BeautifulSoup# 使用默认的 html.parser 解析器
html_doc = "<html></html>"
soup = BeautifulSoup(html_doc, 'html.parser')
# 只是为了示例  不可运行 以下是复制出来的内容
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都验证过,可直接运行👽👽👽
运行有问题可联系作者评论交流🤭🤭🤭
风是自由的,你也是自由🤠🤠🤠
欢迎一起交流学习☠️☠️☠️
有帮助请留下足迹 一键三连🥰🥰🥰
爬虫大佬勿喷,欢迎指正问题😈😈😈
后面会做一系列的爬虫文章,请持续关注作者🤡🤡🤡。

http://www.lryc.cn/news/539218.html

相关文章:

  • 宝塔面板开始ssl后,使用域名访问不了后台管理
  • 大一计算机的自学总结:前缀树(字典树、Trie树)
  • docker 安装的open-webui链接ollama出现网络错误
  • 未来游戏:当人工智能重构虚拟世界的底层逻辑
  • Redis集群主从切换源码解读
  • javacv将mp4视频切分为m3u8视频并播放
  • Golang学习笔记_33——桥接模式
  • 蜂鸟视图发布AI智能导购产品:用生成式AI重构空间服务新范式
  • AI服务器散热黑科技:让芯片“冷静”提速
  • 数据结构-栈、队列、哈希表
  • 安装海康威视相机SDK后,catkin_make其他项目时,出现“libusb_set_option”错误的解决方法
  • 【鸿蒙】ArkUI-X跨平台问题集锦
  • 大模型驱动的业务自动化
  • ocr智能票据识别系统|自动化票据识别集成方案
  • [数据结构]红黑树,详细图解插入
  • 【机器学习】超参数调优指南:交叉验证,网格搜索,混淆矩阵——基于鸢尾花与数字识别案例的深度解析
  • Burp Suite基本使用(web安全)
  • React实现自定义图表(线状+柱状)
  • 从低清到4K的魔法:FlashVideo突破高分辨率视频生成计算瓶颈(港大港中文字节)
  • Qt的QTabWidget的使用
  • Next.js【详解】获取数据(访问接口)
  • 反向代理模块kd
  • leaflet前端初始化项目
  • CMS DTcms 靶场(弱口令、文件上传、tasklist提权、开启远程桌面3389、gotohttp远程登录控制)
  • Docker 入门与实战:从安装到容器管理的完整指南
  • git删除本地分支
  • spring cloud gateway限流常见算法
  • 本地使用docker部署DeepSeek大模型
  • C++ 设计模式-外观模式
  • 【Linux网络编程】应用层协议HTTP(请求方法,状态码,重定向,cookie,session)