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

数据提取之bs4(BeautifuSoup4)模块与Css选择器

from bs4 import BeautifulSoup

创建对象 <class 'bs4.BeautifulSoup'>

soup = BeautifulSoup(源码, '解析器')

bs4标签种类

(1)tag: 标签

print(soup.title, type(soup.title))

(2)获取标签里面的文本内容, 可导航的字符串,数据类型是<class 'bs4.element.NavigableString'>对象,可以使用字符串的方法

title = soup.title
# string
print(title.string, type(title.string))

(3)注释

# 注释 <class 'bs4.element.Comment'>
html = '<b><!--注释内容--></b>'
soup2 = BeautifulSoup(html, 'lxml')

遍历文档树

# 解析数据
head_tag = soup.p #默认获取第一个p标签
# 获取标签的子节点, .contents: 返回的是一个所有子节点的列表
# print(head_tag.contents)

print(head_tag.children)  # 返回的是一个生成器对象,通过循环遍历取值
for head in head_tag.children:
print(head)

源码:

# 1. 导入模块
from bs4 import BeautifulSoup# 源码
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p><p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p><p class="story">...</p>
"""# 2. 创建对象  <class 'bs4.BeautifulSoup'>
soup = BeautifulSoup(html_doc, 'lxml')# 3. 解析数据
head_tag = soup.p
# 获取标签的子节点, .contents: 返回的是一个所有子节点的列表
# print(head_tag.contents)
print(head_tag.children)  # 返回的是一个生成器对象,通过循环遍历
for head in head_tag.children:print(head)

获取节点文本内容

# 通过上一级标签,去获取子级的标签文本内容
# head = soup.head
# print(head.string)


# print(head.text)  # 获取的是多个子级标签的文本内容,内容都拼接在一起

 

# strings/stripped_strings
contents = soup.html
# print(contents.string)  # 没有获取
# print(contents.text)
# print(contents.strings)  # <generator object Tag._all_strings at 0x000001E214912820> 生成器对象
# strings可以获取这个标签下的所有的文本,文本内容包含很多空行
# for data in contents.strings:
#     print(data)

# stripped_strings可以获取这个标签下的所有的文本,去除了多空行
# for data in contents.stripped_strings:
#     print(data)

总结:

获取标签文本内容
string: 标签里面只有一个标签有文本内容, 可导航的字符串
text: 将所有的标签文本内容拼接在一起
strings: 依次获取所有的标签文本内容,包含空行, 返回的是一个生成器对象
stripped_strings: 依次获取所有的标签文本内容,去除多余的空行看, 返回的是一个生成器对象

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

相关文章:

  • Sigma-Aldrich细胞培养基础知识:细胞培养的安全注意事项
  • centos7 安装jenkins
  • 百度文心ERNIE4.5部署与性能白皮书:FastDeploy加速方案+全系列模型实测数据对比
  • Shader面试题100道之(61-80)
  • Django--01基本请求与响应流程
  • 个体户核定多地暂停,将不再享受核定征收?
  • 二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
  • 专题:2025供应链数智化与效率提升报告|附100+份报告PDF、原数据表汇总下载
  • 2025企业私有化知识库工具选型指南——标普智元深度解读
  • 多商户商城系统源码选型指南:开源 vs 定制,哪种更适合?
  • 第七讲:C++中的string类
  • [实战]调频(FM)和调幅(AM)信号生成(完整C语言实现)
  • java多线程环境下资源隔离机制ThreadLocal详解
  • 【C++】红黑树的底层思想 and 大厂面试常问
  • Web前端:table标签的用法与属性
  • 学习日记-spring-day45-7.10
  • 二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
  • LFU 缓存
  • iOS APP混合开发性能测试怎么做?页面卡顿、通信异常的工具组合实战
  • iOS Widget 开发-7:TimelineProvider 机制全解析:构建未来时间线
  • 快速上手ASP .NET Core 8与MongoDB整合
  • Mac 电脑crontab执行定时任务【Python 实战】
  • 【保姆级喂饭教程】idea中安装Conventional Commit插件
  • Wsl/InstallDistro/Service/RegisterDistro/CreateVm/HCS/E_INVALIDARG
  • Android ViewBinding 使用与封装教程​​
  • Flutter 与 Android 的互通几种方式
  • 第35周—————糖尿病预测模型优化探索
  • 灰度发布过程中的异常处理
  • frp内网穿透下创建FTP(解决FTP“服务器回应不可路由的地址。使用服务器地址替代”错误)
  • Vue响应式原理五:响应式-自动收集依赖