python BeautifulSoup的简单使用
官网:https://www.crummy.com/software/BeautifulSoup/bs4/doc/
参考:https://www.cnblogs.com/yupeng/p/3362031.html
什么是BeautifulSoup?
BeautifulSoup是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖析树(parse tree)。 它提供简单又常用的导航(navigating),搜索以及修改剖析树的操作。
下面通过一个测试例子简单说明下BeautifulSoup的用法
from bs4 import BeautifulSoup
def beautifulSoup_test(self):
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
<div class="text" id="div1">测试</div>
and they lived at the bottom of a well.</p> <p class="story">...</p> """
# soup 就是BeautifulSoup处理格式化后的字符串
soup = BeautifulSoup(html_doc,'lxml') # 得到的是title标签
print(soup.title)
# 输出:<title>The Dormouse's story</title> # 得到的是文档中的第一个p标签,要想得到所有标签,得用find_all函数。
# find_all 函数返回的是一个序列,可以对它进行循环,依次得到想到的东西.
print(soup.p)
print(soup.find_all('p')) print(soup.find(id='link3')) # 是返回文本,这个对每一个BeautifulSoup处理后的对象得到的标签都是生效的
print(soup.get_text()) aitems = soup.find_all('a')
# 获取标签a的链接和id
for item in aitems:
print(item["href"],item["id"]) # 1、通过css查找
print(soup.find_all("a", class_="sister"))
# 输出:[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] print(soup.select("p.title"))
# 输出:[<p class="title"><b>The Dormouse's story</b></p>] # 2、通过属性进行查找
print(soup.find_all("a", attrs={"class": "sister"}))
#输出:[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] # 3、通过文本进行查找
print(soup.find_all(text="Elsie"))
# 输出:['Elsie'] print(soup.find_all(text=["Tillie", "Elsie", "Lacie"]))
# 输出:['Elsie', 'Lacie', 'Tillie'] # 4、限制结果个数
print(soup.find_all("a", limit=2))
#输出:[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>] print(soup.find_all(id="link2"))
# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>] print(soup.find_all(id=True))
#输出:[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# 输出:<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>,
# <div class="text" id="div1">测试</div>]
python BeautifulSoup的简单使用的更多相关文章
- Python BeautifulSoup 简单笔记
Beautiful Soup 是用 Python 写的一个 HTML/XML 的解析器,它可以很好的处理不规范标记并生成剖析树.通常用来分析爬虫抓取的web文档.对于 不规则的 Html文档,也有很多 ...
- 爬虫基础库之beautifulsoup的简单使用
beautifulsoup的简单使用 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: ''' Beautiful Soup提供一些简单的.p ...
- python BeautifulSoup的使用方法
BeautifulSoup的使用 我们学习了正则表达式的相关用法,但是一旦正则写的有问题,可能得到的就不是我们想要的结果了,而且对于一个网页来说,都有一定的特殊的结构和层级关系,而且很多标签都有id或 ...
- 用Python写一个简单的Web框架
一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...
- 【转】Python BeautifulSoup 中文乱码解决方法
这篇文章主要介绍了Python BeautifulSoup中文乱码问题的2种解决方法,需要的朋友可以参考下 解决方法一: 使用python的BeautifulSoup来抓取网页然后输出网页标题,但是输 ...
- Python django实现简单的邮件系统发送邮件功能
Python django实现简单的邮件系统发送邮件功能 本文实例讲述了Python django实现简单的邮件系统发送邮件功能. django邮件系统 Django发送邮件官方中文文档 总结如下: ...
- python shutil模块简单介绍
python shutil模块简单介绍 简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作. shutil 模块方法: copy(src, ...
- python之pandas简单介绍及使用(一)
python之pandas简单介绍及使用(一) 一. Pandas简介1.Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据 ...
- python之simplejson,Python版的简单、 快速、 可扩展 JSON 编码器/解码器
python之simplejson,Python版的简单. 快速. 可扩展 JSON 编码器/解码器 simplejson Python版的简单. 快速. 可扩展 JSON 编码器/解码器 编码基本的 ...
随机推荐
- Guice 依赖绑定
Guice 依赖绑定 连接绑定(Linked Bingdings) 连接绑定是 Guice 最基本的一种绑定方式.这种绑定方式我们需要在自己定义的 Module 的 configure() 中编写绑定 ...
- 【AtCoder】CODE FESTIVAL 2017 qual B
最近不知道为啥被安利了饥荒,但是不能再玩物丧志了,不能颓了 饥荒真好玩 A - XXFESTIVAL CCFESTIVAL #include <bits/stdc++.h> #define ...
- Mac上c语言连接mysql遇到的问题
参照<Beginning Linux Programming>上的例程写了一个连接mysql的c语言小程序connect1.c.但是按照书上的编译命令无法编译.然后经过查阅资料解决了问题. ...
- zstu-4243 牛吃草
贴一发两圆相交面积模板 #include<bits/stdc++.h> #define pi acos(-1.0) using namespace std; ; double _abs(d ...
- jquery中方法扩展 ($.fn & $.extend) 学习笔记
A.$.fn 1.$.fn.method() 函数为jQuery对象扩展一个属性和方法(主要用于扩展方法) :method 为自定义方法名 ($.fn 等效 $.prototype) $.fn.bor ...
- 6-17 看图写树 uva10562
非常好的dfs题 有很多细节 关于‘ ’ ‘0’ ’\n‘ 的处理 他们都属于isspace函数 其中 while(buf[x+2][i]=='-'&&buf[x+3][i] ...
- 【Java】 剑指offer(52) 两个链表的第一个公共结点
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 输入两个链表,找出它们的第一个公共结点. 思路 蛮力法:遍历第一个 ...
- hdu 1576 A/B 【扩展欧几里得】【逆元】
<题目链接> <转载于 >>> > A/B Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)( ...
- Java中A instanceof B是什么意思?
instanceof用来判断内存中实际对象A是不是B类型 出现这种情况经常是需要强制转换的时候class Dog extends Animal譬如dog定义了自己的方法wangwang Animal ...
- flask源码剖析
这段时间想重新写个自己的博客系统,又正好在看一些框架源码,然后就想要不顺便写个小框架吧,既然想写框架,要不再顺便写个orm吧,再写个小的异步Server吧..事实证明饭要一口一口吃 先梳理一下flas ...