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. 【优秀的图片后期编辑工具】Luminar 3.1 for Mac

     [简介] 今天和大家分享最新的 Luminar for Mac 3.1 版本,支持中文界面,Luminar是一款Mac上优秀的图片后期处理工具,功能类似 Photoshop Lightroom 等软 ...

  2. NameNode和SecondaryNameNode工作原理剖析

    NameNode和SecondaryNameNode工作原理剖析 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.NameNode中的元数据是存储在那里的? 1>.首先,我 ...

  3. IDEA中debug启动tomcat报错。Error running t8:Unable to open debugger port(127.0.0.1:49225):java.net.BindException"Address alread in use:JVM_Bind"

    解决办法: 1,如下图打开项目配置的tomcat的“Edit Configurations...” 2,打开“Startup/Connection”--------"Debug"- ...

  4. ibatis .net $与#的区别

    $与#的区别 SELECT * FROM TABLE WHERE Id = #id# 其中如果字段id为字符串类型,那么#id#表示的就是'id',也就是说会自动加引号.如果id为整型,那么#id#就 ...

  5. vue 点击展开显示更多 点击收起部分隐藏

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 关于vue中如何配置echarts以及使用方法

    ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,Safari等) ...

  7. 关于JDK1.7+中HashMap对红黑树场景的思考

    背景 在1.7之前的版本,当数组元素较多(几百.几千,或者更多)的时候,在这种前提扩容,涉及全量元素的遍历和坐标的重新定位,这个耗时会比较长.这是之前存在的一个弊端吧.那么引入红黑树之后就解决了问题, ...

  8. MyBatis3

    一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的conf.xml文件中,如下: <?xml version="1 ...

  9. further configuration avilable 不见了

    Dynameic Web Module的further configuration avilable  不见了 打开目录下的 org.eclipse.wst.common.project.facet. ...

  10. python3字符串

    Python3 字符串 Python字符串运算符 + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符串 a*2 输出结果:HelloHello [] 通过索引获取字符串中 ...