Python Beautiful Soup模块使用教程详解
作者:Thunderclap_ 发布时间:2021-07-21 16:00:10
标签:Python,Beautiful,Soup
一、模块简介
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间.
二、方法利用
1、引入模块
# 引入
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" rel="external nofollow" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" rel="external nofollow" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
四种解析器
2、几个简单的浏览结构化数据的方法
#获取Tag,通俗点就是HTML中的一个个标签
#获取Tag,通俗点就是HTML中的一个个标签
soup.title # 获取整个title标签字段:<title>The Dormouse's story</title>
soup.title.name # 获取title标签名称 :title
soup.title.parent.name # 获取 title 的父级标签名称:head
soup.p # 获取第一个p标签字段:<p class="title"><b>The Dormouse's story</b></p>
soup.p['class'] # 获取第一个p中class属性值:title
soup.p.get('class') # 等价于上面
soup.a # 获取第一个a标签字段
soup.find_all('a') # 获取所有a标签字段
soup.find(id="link3") # 获取属性id值为link3的字段
soup.a['class'] = "newClass" # 可以对这些属性和内容等等进行修改
del bs.a['class'] # 还可以对这个属性进行删除
soup.find('a').get('id') # 获取class值为story的a标签中id属性的值
soup.title.string # 获取title标签的值 :The Dormouse's story
三、具体利用
1、获取拥有指定属性的标签
方法一:获取单个属性
soup.find_all('div',id="even") # 获取所有id=even属性的div标签
soup.find_all('div',attrs={'id':"even"}) # 效果同上
方法二:
soup.find_all('div',id="even",class_="square") # 获取所有id=even并且class=square属性的div标签
soup.find_all('div',attrs={"id":"even","class":"square"}) # 效果同上
2、获取标签的属性值
方法一:通过下标方式提取
for link in soup.find_all('a'):
print(link['href']) //等同于 print(link.get('href'))
方法二:利用attrs参数提取
for link in soup.find_all('a'):
print(link.attrs['href'])
3、获取标签中的内容
divs = soup.find_all('div') # 获取所有的div标签
for div in divs: # 循环遍历div中的每一个div
a = div.find_all('a')[0] # 查找div标签中的第一个a标签
print(a.string) # 输出a标签中的内容
如果结果没有正确显示,可以转换为list列表
4、stripped_strings
去除\n换行符等其他内容 stripped_strings
divs = soup.find_all('div')
for div in divs:
infos = list(div.stripped_strings) # 去掉空格换行等
bring(infos)
四、输出
1、格式化输出prettify()
prettify() 方法将Beautiful Soup的文档树格式化后以Unicode编码输出,每个XML/HTML标签都独占一行
markup = '<a href="http://example.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >I linked to <i>example.com</i></a>'
soup = BeautifulSoup(markup)
soup.prettify()
# '<html>\n <head>\n </head>\n <body>\n <a href="http://example.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >\n...'
print(soup.prettify())
# <html>
# <head>
# </head>
# <body>
# <a href="http://example.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
# I linked to
# <i>
# example.com
# </i>
# </a>
# </body>
# </html>
2、get_text()
如果只想得到tag中包含的文本内容,那么可以调用 get_text() 方法,这个方法获取到tag中包含的所有文版内容包括子孙tag中的内容,并将结果作为Unicode字符串返回:
markup = '<a href="http://example.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >\nI linked to <i>example.com</i>\n</a>'
soup = BeautifulSoup(markup)
soup.get_text()
u'\nI linked to example.com\n'
soup.i.get_text()
u'example.com'
来源:https://blog.csdn.net/Thunderclap_/article/details/128948135


猜你喜欢
- 在上一篇Python接口自动化测试系列文章:Python接口自动化浅析登录接口测试实战,主要介绍接口概念、接口用例设计及登录接口测试实战。以
- Python中一切都是对象。类提供了创建新类型对象的机制。这篇教程中,我们不谈类和面向对象的基本知识,而专注在更好地理解Python面向对象
- 本文实例讲述了Django rest framework工具包简单用法。分享给大家供大家参考,具体如下:Django rest framew
- 使用Python3和Opencv识别一张标准的答题卡。大致的过程如下:1.读取图片2.利用霍夫圆检测,检测出四个角的黑圆位置,从确定四个角的
- 之前看到过很多头条,说哪国某人坚持了多少年自学使用excel画画,效果十分惊艳。 对于他们的耐心我十分敬佩。 但是作为一个程序员,自然也得挑
- python3.6.4安装opencv3.4.2使用pip安装OpenCV直接安装最新版:pip3 install opencv_pytho
- 整数在Python中,整数(integer)是一种内置数据类型,用于表示整数值。Python中的整数类型可以表示任意大小的整数,而不需要考虑
- 本文实例为大家分享了JS编写简单选项卡的具体代码,供大家参考,具体内容如下<!DOCTYPE html><html lan
- 天气查询python小程序第0步:导入工具库第一步:生成查询天气的url链接第二步:访问url链接,解析服务器返回的json数据,变成pyt
- 本文实例讲述了python查找指定具有相同内容文件的方法。分享给大家供大家参考。具体如下:python代码用于查找指定具有相同内容的文件,可
- go build 报错:main.go:5:2: cannot find package “gopkg.in/go-playground/v
- 本文主要介绍了Python通过tkinter实现百度搜索的示例代码,分享给大家,具体如下:"""百度搜索可视化
- 散点图什么是散点图?散点图是指在数理统计回归分析中,数据点在直角坐标系平面上的分布图, 散点图表示因变量随自变量而变化的大致趋势,
- 方法一、线程池执行的循环代码为自己写的情况定义一个全局变量,默认为T,当QT界面关闭后,将该变量值改为F。线程执行的循环代码内增加一个判断方
- 关于tensor.repeat()的使用考虑到很多人在学习这个函数,我想在这里提 一个建议:强烈推荐 使用 einops 模块中的 repe
- 前言段(segment)是一种在数据库中消耗物理存储空间的任何实体(一个段可能存在于多个数据文件中,因为物理的数据文件是组成逻辑表空间的基本
- 在数据库应用的设计中,我们往往会需要获取某些表的记录总数,用于判断表的记录总数是否过大,是否需要备份数据等。我们通常的做法是:select
- 基础知识使用框架的优点稳定性和可扩展性强可以降低开发难度,提高了开发效率Flask诞生于2010年,是Armin ronacher用Pyth
- 刚来公司的时候领导给分配的都是一些简单的简单的简单的。。。。。任务一次叫我把文章的字体大小变换功能写出来。在网上搜了很多都不管用!不过功夫不
- 1.什么是Pillow首先我们需要了解一下PIL(Python Imaging Library),它是Python2中非常强大的图像处理标准