bs4解析库
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解析库的更多相关文章
- 爬虫 解析库re,Beautifulsoup,
re模块 点我回顾 Beautifulsoup模块 #安装 Beautiful Soup pip install beautifulsoup4 #安装解析器 Beautiful Soup支持Pytho ...
- Python爬虫【解析库之beautifulsoup】
解析库的安装 pip3 install beautifulsoup4 初始化 BeautifulSoup(str,"解析库") from bs4 import BeautifulS ...
- 解析库之re,Beautifulsoup
本篇导航: 介绍 基本使用 遍历文档树 搜索文档树 总结 re模块在之前的python进阶中有讲过不再做过多的阐述,本篇为BeautifulSoup库的分析 20.collections模块和 ...
- 爬虫模块介绍--Beautifulsoup (解析库模块,正则)
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时 ...
- Python3编写网络爬虫06-基本解析库Beautiful Soup的使用
二.Beautiful Soup 简介 就是python的一个HTML或XML的解析库 可以用它来很方便的从网页中提取数据 0.1 提供一些简单的 python式的函数来处理导航,搜索,修改分析树等功 ...
- python3解析库BeautifulSoup4
Beautiful Soup是python的一个HTML或XML的解析库,我们可以用它来方便的从网页中提取数据,它拥有强大的API和多样的解析方式. Beautiful Soup的三个特点: Beau ...
- Python爬虫之Beautiful Soup解析库的使用(五)
Python爬虫之Beautiful Soup解析库的使用 Beautiful Soup-介绍 Python第三方库,用于从HTML或XML中提取数据官方:http://www.crummv.com/ ...
- 爬虫----爬虫解析库Beautifulsoup模块
一:介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你 ...
- 【Python爬虫】BeautifulSoup网页解析库
BeautifulSoup 网页解析库 阅读目录 初识Beautiful Soup Beautiful Soup库的4种解析器 Beautiful Soup类的基本元素 基本使用 标签选择器 节点操作 ...
随机推荐
- 贝叶斯A/B测试 - 一种计算两种概率分布差异性的方法过程
1. 控制变量 0x1:控制变量主要思想 科学中对于多因素(多变量)的问题,常常采用控制因素(变量)的方法,吧多因素的问题变成多个单因素的问题.每一次只改变其中的某一个因素,而控制其余几个因素不变,从 ...
- 安装python的pywin32安装不了,提示找不到py3.6-32
安装python的pywin32安装不了,提示找不到py3.6-32 首先我自己的py3.6是64位版本的,这是pywin32模块的下载地址 里面有各种版本的,首先我先下了64位的3.6版本的,结果提 ...
- showdoc 文档管理系统
==========================showdoc 简介==========================在线文档管理系统很多, 比如阿里的语雀.腾讯的 TAPD 平台也包括文档管理 ...
- XXX系统项目分析
目标: 实现网上需求征集与审核. 好处: (1)网上填报不受时间和地点限制: (2)流程简单明确,节省人力物力: (3)信息存储,查询,筛选远比纸质材料方便: (4)方便统计,分析数据: 度量标准: ...
- Linux基础 - 系统优化及常用命令
目录 Linux基础系统优化及常用命令 Linux基础系统优化 网卡配置文件详解 ifup,ifdown命令 ifconfig命令 ifup,ifdown命令 ip命令 用户管理与文件权限篇 创建普通 ...
- lambda+mutable配合move实现单函数多程序域
主代码 //-----------------------------------说明一的代码 void fun0{ int t = 10; auto loopFun = [=]() mutable{ ...
- linux 工具
zsh: ubuntu安装: sudo apt-get install zsh
- nginx做80端口转发
server { server_name zjrzb.cn listen 80; location / { proxy_pass http://127.0.0.1:8090; proxy_set_he ...
- cout,cerr和clog的区别
官方解释: cout——Standard output stream Object of class ostream that represents the standard output strea ...
- 基于Python的Webservice开发(二)-如何用Spyne开发Webservice
一.功能需求 本次案例是开发一个Item的新建的WebService.IN&OUT的类型JsonDocument. 通过传入相关的参数创建Item,且相关的参数可以被缺省. 二.实现代码 引入 ...