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类的基本元素 基本使用 标签选择器 节点操作 ...
随机推荐
- fork子进程
title: fork子进程 data: 2019/3/21 20:24:39 toc: true --- 这里实在学习socket编程前的小知识点,用来创建多个服务端 学习文档 函数可以有两个返回值 ...
- Telegraf+InfluxDB+Grafana搭建服务器监控平台
Telegraf+InfluxDB+Grafana搭建服务器监控平台 tags:网站 个人网站:https://wanghualong.cn/ 效果展示 本站服务器状态监控:https://statu ...
- H5_0008:链接分享图片和判断平台
<!--分享图片--><div id="share_img" style="display:none;"><img class=& ...
- 078、Docker 最常用的监控方案(2019-04-25 周四)
参考https://www.cnblogs.com/CloudMan6/p/7637361.html 当 Docker 部署规模逐步变大后,可视化监控容器环境的性能和健康状态会变得越来越重要. ...
- 光刻技术的原理和EUV光刻技术前景
本文转载自微信公众号 半导体技术天地, 链接 https://mp.weixin.qq.com/s/EEBkSQ_Yc8RYFO18VpO8ow
- python 学习 leetcode ---number of island
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- ccf 201503-5 最小花费 这题交上去只有10分嗨!求大佬的题解啊
问题描述 C国共有n个城市.有n-1条双向道路,每条道路连接两个城市,任意两个城市之间能互相到达.小R来到C国旅行,他共规划了m条旅行的路线,第i条旅行路线的起点是si,终点是ti.在旅行过程中,小R ...
- HDU-1028 Ignatius and the Princess III(生成函数)
题意 给出$n$,问用$1$到$n$的数字问能构成$n$的方案数 思路 生成函数基础题,$x^{n}$的系数即答案. 代码 #include <bits/stdc++.h> #define ...
- 更改checkbox的默认样式
最近做一个vue项目要用到checkbox要修改默认样式,选中是纯白色,不选择只有白色边框,起初以为很容易,没想到还折腾了一翻,记录一下. 几经折腾,理清input 和label的关系 最终改进版本, ...
- HG奋斗赛A[20190428]
T1 很简单,判断这个字符串有多少个不同的字符,让后用k减一减 注意: 1.如果不同字符数大于k,不要输出负数 2.变量名别打错 上代码 #include <cstdio> #includ ...