beautifulsoup4

bs4解析库是灵活又方便的网页解析库,处理高效,支持多种解析器。利用它不用编写正则表达式即可方便地实现网页的提取

要解析的html标签

from bs4 import BeautifulSoup

# 要解析的html标签
html_str = """
<li data_group="server" class="content">
<a href="/commands.html" class="index" name="a1">第一个a标签
<a href="/commands.html" class="index2" name="a2">第二个a标签
<a href="/commands/flushdb.html">
<span class="first">
这是第一个span标签
<span class="second">
这是第二个span标签,第一个下的子span标签
</span>
</span>
<span class="third">这是第三个span标签</span>
<h3>这是一个h3</h3>
</a>
</li>
"""

1. 找标签:

# 1. find_all 找到所有的li标签 结果为一个结果集
li_find_all = BeautifulSoup(html_str, "lxml").find_all("li")
print(type(li_find_all)) # <class 'bs4.element.ResultSet'>
# 2. find 找到第一个li标签 结果为一个标签对象
li_find = BeautifulSoup(html_str, "lxml").find("li")
print(type(li_find)) # <class 'bs4.element.Tag'>
# 添加限制条件 class id
li = BeautifulSoup(html_str, "lxml").find_all("li", class_="content", data_group="server")
li1 = BeautifulSoup(html_str, "lxml").find_all("li", attrs={"class":"content", "data_group":"server"})

2. 找标签属性和name:

# 找到a标签的属性和name
a = BeautifulSoup(html_str, "lxml").find("a")
print(a.get("href"), a.name, type(a.get("href"))) # /commands.html a <class 'str'>
print(a.attrs, type(a.attrs), a.text, a.string,a.get_text(), type(a.string))
# {'href': '/commands.html', 'class': ['index'], 'name': 'a1'} <class 'dict'> 第一个a标签 <class 'bs4.element.NavigableString'>

3. 处理子标签和后代标签:

# 找到li下的后代标签
li_find = BeautifulSoup(html_str, "lxml").find("li")
print(li_find.children) # <list_iterator object at 0x00000132C0915320>
"""
for i in li_find.children:
print(type(i),i)
"""
# 找到li下的子标签 返回第一个找到的标签
print(li_find.a, type(li_find.a))
# <a class="index" href="/commands.html" name="a1">第一个a标签</a> <class 'bs4.element.Tag'>

4. 处理兄弟标签:

# 处理a标签的兄弟
a = BeautifulSoup(html_str, "lxml").find("a", class_="index2")
print(a.next_siblings, type(a.next_siblings)) # <generator object next_siblings at 0x000001B14AA712B0> <class 'generator'>
"""
for i in a.next_siblings:
print(i, type(i), "\n")
1. <a class="index" href="/commands.html" name="a1">第一个a标签
</a> <class 'bs4.element.Tag'>
2. <a href="/commands/flushdb.html">
<span class="first">
这是第一个span标签
<span class="second">
这是第二个span标签,第一个下的子span标签
</span>
</span>
<span class="third">这是第三个span标签</span>
<h3>这是一个h3</h3>
</a> <class 'bs4.element.Tag'>
"""
# print("next--", a.last ,type(a.next))
# 一组兄弟标签中的下一个标签next_sibling() 下的所有标签next_siblings()
# 一组兄弟标签中的上一个标签previous_sibling() 上的所有标签previous_siblings()
# 找到一组兄弟标签下的最后一个标签:
a = [x for x in a.next_siblings][-1]
print("aaaaaa", a, type(a))

5. 处理父标签:

# 1.parent # 返回的父标签及其子标签
span = BeautifulSoup(html_str, "lxml").find("span", class_="second")
print(span.parent, type(span.parent))
# 2. parents 一层一层返回
"""
span = BeautifulSoup(html_str, "lxml").find("span", class_="second")
for i in span.parents:
print(i)
"""

6. 标签的其它一些处理方法

# 1. prettify方法
# 这个方法就是在每个标签后加入一个\n 打印出来是十分规范的h5代码 一目了然
# 也可以对某个标签做格式化处理
a = BeautifulSoup(html_str, "lxml").find("a")
print(a.prettify()) # 2.contents方法
li = BeautifulSoup(html_str, "lxml")
print(li.contents, type(li.contents))
print(li.childrent, type(li.children))
"""
li_find.contents 返回的是一个列表 查找的标签下的子标签 包括'\n'
li_find.children 返回的是一个迭代器, 迭代器的内容与li_find.contents一样
"""

bs4解析库的更多相关文章

  1. 爬虫 解析库re,Beautifulsoup,

    re模块 点我回顾 Beautifulsoup模块 #安装 Beautiful Soup pip install beautifulsoup4 #安装解析器 Beautiful Soup支持Pytho ...

  2. Python爬虫【解析库之beautifulsoup】

    解析库的安装 pip3 install beautifulsoup4 初始化 BeautifulSoup(str,"解析库") from bs4 import BeautifulS ...

  3. 解析库之re,Beautifulsoup

    本篇导航: 介绍 基本使用 遍历文档树 搜索文档树 总结     re模块在之前的python进阶中有讲过不再做过多的阐述,本篇为BeautifulSoup库的分析 20.collections模块和 ...

  4. 爬虫模块介绍--Beautifulsoup (解析库模块,正则)

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时 ...

  5. Python3编写网络爬虫06-基本解析库Beautiful Soup的使用

    二.Beautiful Soup 简介 就是python的一个HTML或XML的解析库 可以用它来很方便的从网页中提取数据 0.1 提供一些简单的 python式的函数来处理导航,搜索,修改分析树等功 ...

  6. python3解析库BeautifulSoup4

    Beautiful Soup是python的一个HTML或XML的解析库,我们可以用它来方便的从网页中提取数据,它拥有强大的API和多样的解析方式. Beautiful Soup的三个特点: Beau ...

  7. Python爬虫之Beautiful Soup解析库的使用(五)

    Python爬虫之Beautiful Soup解析库的使用 Beautiful Soup-介绍 Python第三方库,用于从HTML或XML中提取数据官方:http://www.crummv.com/ ...

  8. 爬虫----爬虫解析库Beautifulsoup模块

    一:介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你 ...

  9. 【Python爬虫】BeautifulSoup网页解析库

    BeautifulSoup 网页解析库 阅读目录 初识Beautiful Soup Beautiful Soup库的4种解析器 Beautiful Soup类的基本元素 基本使用 标签选择器 节点操作 ...

随机推荐

  1. win10 右下角显示秒

    点击Contana搜索框,输入“regedit”打开注册表编辑器: 查找:HKEY_CURRENT_USER/SOFTWARE/Microsoft/Windows/CurrentVersion/Exp ...

  2. current account(经常账户)

    python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...

  3. js 时间格式化 兼容safari 苹果手机

    export function formatTime (fmt, date) { date = new Date(date + '+08:00') // 兼容safari var o = { 'M+' ...

  4. C# NPOI 操作Excel 案例

    1.加入NPOI 程序集,使用nuget添加程序集 2.引用NPOI程序集 private IWorkbook ExportExcel(PrintQuotationOrderViewModel mod ...

  5. iTOP-iMX6UL开发板【全能版】-动态调频技术简介

    本文档以 iMX6UL 为例,简单介绍 cpufreq 的 5 种模式. 在 imx6ul 的 menuconfig 中,进入 CPU Power Management ---> CPU Fre ...

  6. Flsk-Bootstrap-2

    目录 Flsk-Bootstrap-2 结构 解压Bootstrap 制作基础模板 视图函数 初始文件 启动文件 浏览器 Flsk-Bootstrap-2 参考:Flask 项目中使用 bootstr ...

  7. STL--sort源码分析

    SGI STL sort源码 temlate <class RandowAccessIterator> inline void sort(RandowAccessIterator firs ...

  8. python&django 实现页面中关联查询小功能(中级篇)

    目的 组合搜索实现如下图功能 知识点 1.使用自定义标签模板(templatetags) 实现 models.py 和 views.py和初级篇一样 重点如下:在app01目录下创建templatet ...

  9. python中的MySQL使用 + pickle使用

    (1)python中有一个包“sqlite3”,可以用来进行数据库相关的操作: 参考下面一个例子: import sqlite3 import pickle img_list = [('a' , 0) ...

  10. [原创] f2fs文件系统源代码分析 —— 基于3.8内核 (一)

    作者:高翔 <esxgx@163.com>本文著作权归作者所有,请在转载引用时保留原文网址. 在全文开始,首先记录f2fs被3.8主线merge的mailing list:[GIT PUL ...