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. golang-flag的问题

    如果选择-flag x 就是不支持布尔型

  2. npm_config_

    npm script时会带一些参数变量,例如: "test": "node scripts/tools/test.js --name=test111" 平常我们 ...

  3. C#多线程处理

    创建多线程,并带参数! using System; using System.Collections; using System.Collections.Generic; using System.I ...

  4. python-类内置属性和内置方法

    class A(): ''' 这是一个类 ''' banji=1 def __init__(self,name,age): self.name=name self.age=age def AA(sel ...

  5. Nodejs一键实现微信内打开网页url自动跳转外部浏览器访问的功能

    前言 现如今微信对第三方推广链接的审核是越来越严格了,域名在微信中分享转发经常会被拦截,一旦被拦截用户就只能复制链接手动打开浏览器粘贴才能访问,要不然就是换个域名再推,周而复始.无论是哪一种情况都会面 ...

  6. PyCharm(python的开发工具)的安装与破解

    最近在进行python的入门学习,俗话说:工欲善其事,必先利其器.最初学习时,一款好的IDE(Integrated Development Environment)绝对是很重要的,有利于后期学习,并且 ...

  7. C# 用Serializer.ToXml()方法转换成两种格式的XML

    常见XML格式两种: 这种是属性的格式,实体的Model属性上面加上这个特性 [XmlAttribute] <AAA aa="/> 这种是标签的格式,实体的Model属性上面加上 ...

  8. git本机服务器配置(三):Gitblit的安装

    1. 下载 http://www.gitblit.com/ 2. 解压下载文件 3. 配置信息 3.1 需要提前配置好java jdk环境 3.2 打开data目录下的defaults.propert ...

  9. Mac环境下Vagrant的安装

    1.安装Vagrant 下载地址:https://www.vagrantup.com/downloads.html 下载好pkg包后,点击安装即可. 2.安装 VirtualBox 下载地址:http ...

  10. 【转】一文掌握 Linux 性能分析之 CPU 篇

    [转]一文掌握 Linux 性能分析之 CPU 篇 平常工作会涉及到一些 Linux 性能分析的问题,因此决定总结一下常用的一些性能分析手段,仅供参考. 说到性能分析,基本上就是 CPU.内存.磁盘 ...