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

python 爬虫篇(3)---->Beautiful Soup 网页解析库的使用(包含实例代码)

Beautiful Soup 网页解析库的使用

文章目录

  • Beautiful Soup 网页解析库的使用
  • 前言
  • 一、安装Beautiful Soup 和 lxml
  • 二、Beautiful Soup基本使用方法
    • 标签选择器
      • 1 .string --获取文本内容
      • 2 .name --获取标签本身名称
      • 3 .attrs[] --通过属性拿属性的值
    • 标准选择器
      • find_all( name , attrs , recursive , text , **kwargs )
        • text=() 根据文本值选择
      • find( name , attrs , recursive , text , **kwargs )
    • CSS选择器
      • 获取属性的值
  • 总结


前言

大家好,今天我们来说一说爬虫中比较常用的一种解析数据的网页解析库—Beautiful Soup 的使用.可以从html网页中提取出我们想要的数据,比re正则方便而且还不容易出错,所以是爬虫中的得力助手.

beautiful soup支持多种解析器 例如’XML’ .‘HTML’ 等等

以下是beautiful soup所支持的解析器

在这里插入图片描述


一、安装Beautiful Soup 和 lxml

win + r 输入cmd 在终端中输入: pip install BeautifulSoup4 再输入 pip install lxml

注意: 如果出现爆红 或者警告 尝试使用镜像源下载 (以前的文章说过)


二、Beautiful Soup基本使用方法

标签选择器

1 .string --获取文本内容

h = """
<html><head><title>The Dormouse's story</title></head><body><p class="title" name="dromouse"><b><span>The Dormouse's story</span></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>
"""# 1,导包
from bs4 import BeautifulSoup 
#,2,实例化对象
soup = BeautifulSoup(h, 'lxml')  # 参数1:要解析的内容  参数2:解析器# 通过标签选取,会返回包含标签本身及其里面的所有内容
print(soup.head) # 包含head标签在内的所有内容
print(soup.p) # 返回匹配的第一个结果# .string是属性,作用是获取字符串文本
print(soup.title.string)

运行结果:

	<head><title>The Dormouse's story</title></head><p class="title" name="dromouse"><b><span>The Dormouse's story</span></b></p>The Dormouse's story

2 .name --获取标签本身名称

html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><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="siste
http://www.lryc.cn/news/300591.html

相关文章:

  • 第十二周学习报告
  • Redis面试题整理(持续更新)
  • 一周学会Django5 Python Web开发-Django5 Hello World编写
  • 讲解用Python处理Excel表格
  • WEB APIs(1)
  • C++重新入门-基本输入输出
  • 【C语言】解析刘谦春晚魔术《守岁共此时》
  • 剑指offer——数值的整数次方
  • Tied Block Convolution: 具有共享较薄滤波器的更简洁、更出色的CNN
  • 算法沉淀——BFS 解决 FloodFill 算法(leetcode真题剖析)
  • wordpress外贸成品网站模板
  • 如何使用六图一表七种武器
  • 阿里云游戏服务器租用费用价格组成,费用详单
  • 【C++】C++11上
  • 【前端高频面试题--git篇】
  • c++创建对象
  • 软件实例分享,洗车店系统管理软件会员卡电子系统教程
  • 【Docker进阶】镜像制作-用Dockerfile制作镜像(一)
  • 数据密集型应用系统设计
  • 分布式文件系统 SpringBoot+FastDFS+Vue.js【一】
  • 【PyQt】11-QTextEdit、QPushButton
  • 初识webpack(二)解析resolve、插件plugins、dev-server
  • 什么是自编码器Auto-Encoder?
  • openGauss学习笔记-219 openGauss性能调优-确定性能调优范围-硬件瓶颈点分析-网络
  • SAP PP学习笔记- 豆知识01 - 怎么查询既存品目
  • 相机的机身马达有什么用?
  • 拿捏c语言指针(上)
  • JVM指令手册
  • Linux之多线程
  • TestNG invocationCount属性