官网:https://www.crummy.com/software/BeautifulSoup/bs4/doc/

  参考:https://www.cnblogs.com/yupeng/p/3362031.html

  

  什么是BeautifulSoup?

    BeautifulSoup是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖析树(parse tree)。 它提供简单又常用的导航(navigating),搜索以及修改剖析树的操作。

  下面通过一个测试例子简单说明下BeautifulSoup的用法

  

from bs4 import BeautifulSoup
def beautifulSoup_test(self):
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>;
<div class="text" id="div1">测试</div>
and they lived at the bottom of a well.</p> <p class="story">...</p> """
# soup 就是BeautifulSoup处理格式化后的字符串
soup = BeautifulSoup(html_doc,'lxml') # 得到的是title标签
print(soup.title)
# 输出:<title>The Dormouse's story</title> # 得到的是文档中的第一个p标签,要想得到所有标签,得用find_all函数。
# find_all 函数返回的是一个序列,可以对它进行循环,依次得到想到的东西.
print(soup.p)
print(soup.find_all('p')) print(soup.find(id='link3')) # 是返回文本,这个对每一个BeautifulSoup处理后的对象得到的标签都是生效的
print(soup.get_text()) aitems = soup.find_all('a')
# 获取标签a的链接和id
for item in aitems:
print(item["href"],item["id"]) # 1、通过css查找
print(soup.find_all("a", class_="sister"))
# 输出:[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] print(soup.select("p.title"))
# 输出:[<p class="title"><b>The Dormouse's story</b></p>] # 2、通过属性进行查找
print(soup.find_all("a", attrs={"class": "sister"}))
#输出:[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] # 3、通过文本进行查找
print(soup.find_all(text="Elsie"))
# 输出:['Elsie'] print(soup.find_all(text=["Tillie", "Elsie", "Lacie"]))
# 输出:['Elsie', 'Lacie', 'Tillie'] # 4、限制结果个数
print(soup.find_all("a", limit=2))
#输出:[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>] print(soup.find_all(id="link2"))
# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>] print(soup.find_all(id=True))
#输出:[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# 输出:<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>,
# <div class="text" id="div1">测试</div>]

python BeautifulSoup的简单使用的更多相关文章

  1. Python BeautifulSoup 简单笔记

    Beautiful Soup 是用 Python 写的一个 HTML/XML 的解析器,它可以很好的处理不规范标记并生成剖析树.通常用来分析爬虫抓取的web文档.对于 不规则的 Html文档,也有很多 ...

  2. 爬虫基础库之beautifulsoup的简单使用

    beautifulsoup的简单使用 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: ''' Beautiful Soup提供一些简单的.p ...

  3. python BeautifulSoup的使用方法

    BeautifulSoup的使用 我们学习了正则表达式的相关用法,但是一旦正则写的有问题,可能得到的就不是我们想要的结果了,而且对于一个网页来说,都有一定的特殊的结构和层级关系,而且很多标签都有id或 ...

  4. 用Python写一个简单的Web框架

    一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...

  5. 【转】Python BeautifulSoup 中文乱码解决方法

    这篇文章主要介绍了Python BeautifulSoup中文乱码问题的2种解决方法,需要的朋友可以参考下 解决方法一: 使用python的BeautifulSoup来抓取网页然后输出网页标题,但是输 ...

  6. Python django实现简单的邮件系统发送邮件功能

    Python django实现简单的邮件系统发送邮件功能 本文实例讲述了Python django实现简单的邮件系统发送邮件功能. django邮件系统 Django发送邮件官方中文文档 总结如下: ...

  7. python shutil模块简单介绍

    python shutil模块简单介绍 简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作. shutil 模块方法: copy(src, ...

  8. python之pandas简单介绍及使用(一)

    python之pandas简单介绍及使用(一) 一. Pandas简介1.Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据 ...

  9. python之simplejson,Python版的简单、 快速、 可扩展 JSON 编码器/解码器

    python之simplejson,Python版的简单. 快速. 可扩展 JSON 编码器/解码器 simplejson Python版的简单. 快速. 可扩展 JSON 编码器/解码器 编码基本的 ...

随机推荐

  1. js获取iframe中的元素

    var obj=document.getElementById("iframe的name").contentWindow; var ifmObj=obj.document.getE ...

  2. poj1177 矩形周长并

    线段树扫描线的模板题,一个月前写的发现忘了一些还是要看看以前的博客呀! /* 思路:数据小不用离散化处理,线段树叶子结点维护一个区间 */ #include<iostream> #incl ...

  3. js字符串转换成数字与数字转换成字符串的实现方法

    转载:点击查看地址 js字符串转换成数字 将字符串转换成数字,得用到parseInt函数.parseInt(string) : 函数从string的开始解析,返回一个整数. 举例:parseInt(' ...

  4. docker安装sonarqube及实际应用

    由于平台的多样化,在不同环境的安装方式可能也不一样,为了避免环境不一致带来的差异,特记一笔容器安装: 一.Sonar可以从以下七个维度检测代码质量,而作为开发人员至少需要处理前5种代码质量问题. 1. ...

  5. 对象本田CRV

    <script> window.onload = function (ev) { var vcar = new Car("本田", "CRV", 4 ...

  6. 2018-2019 2 20165203 《网络对抗技术》Exp6 信息搜集与漏洞扫描

    2018-2019 2 20165203 <网络对抗技术>Exp6 信息搜集与漏洞扫描 实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 实践内容 (1)各种搜索技巧的应用 (2) ...

  7. jsTree动态加载数据

    Views代码 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="view ...

  8. Django的auto_now=True没有自动更新

    auto_now=True自动更新,有一个条件,就是要通过django的model层. 如create或是save方法. 如果是filter之后update方法,则直接调用的是sql,不会通过mode ...

  9. [转] 理解 JavaScript 中的 Array.prototype.slice.apply(arguments)

    假如你是一个 JavaScript 开发者,你可能见到过 Array.prototype.slice.apply(arguments) 这样的用法,然后你会问,这么写是什么意思呢? 这个语法其实不难理 ...

  10. 【BZOJ5110】[CodePlus2017]Yazid 的新生舞会

    题解: 没笔的时候我想了一下 发现如果不是出现一半次数而是k次,并不太会做 然后我用前缀和写了一下发现就是维护一个不等式: 于是就可以随便维护了