Python BeautifulSoup4 使用指南
前言:
昨天把传说中的BeautifulSoup4装上了,还没有装好的童鞋,请看本人的上一篇博客:
Python3 Win7安装 BeautifulSoup,依照里面简单的步骤就能够把BeautifulSoup装上啦。非常easy的,表害怕
装好BeautifulSoup4之后,就让我们来好好享受这碗BeautifulSoup吧,哈哈
入门:
以下就来介绍一下BeautifulSoup吧,BeautifulSoup是一个可以从HTML或XML文件里提取数据的Python库.它可以通过你喜欢的转换器实现惯用的文档导航,查找,改动文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间
在你知道有BeautifulSoup这货之前。假设你从一个文本中提取出自己想要的东西,那么预计你应该使用re模块,但先在假设给你一个HTML文件让你提取出一些关键信息,假设再使用re模块,尽管也能够把信息提取出来,可是捏,你可能会绞尽脑汁苦思冥想查阅好多资料。如今,我们有了BeautifulSoup,一切变得超级简单起来,对,就是这么简单
实践:
学习bs4最好的还是查看bs4的官方文档,有中文版的哦,猛点这里官方文档,看起来会非常快,笔者花了大概一个下午的时间,把bs4的官方文档看了一遍,顺手也写了写里面的演示样例程序,假设你没多少时间的话,看看我以下的代码。预计你会非常快上手的。相信我
 (*^_^*)
__author__ = 'MrChen' from bs4 import BeautifulSoup
#这是演示样例
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>;
and they lived at the bottom of a well.</p> <p class="story">...</p>
"""
#初始化,实例化一个BeautifulSoup对象,參数能够是一个字符串,也能够是一个打开的文件比方open('mydoc.html')
soup = BeautifulSoup(html_doc) print(soup.title)
#输出:<title>The Dormouse's story</title> print(soup.title.parent)
#输出:<head><title>The Dormouse's story</title></head> print(soup.title.parent.parent)
#输出:
#<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 class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
#<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
#and they lived at the bottom of a well.</p>
#<p class="story">...</p>
#</body></html> print(soup.title.name)
#输出:title print(soup.title.parent.name)
#输出:head print(soup.title.parent.parent.name)
#输出:html print(soup.p)
#输出:<p class="title"><b>The Dormouse's story</b></p> print(soup.p['class'])
#输出:['title'] print(soup.a)
#输出:<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a> print(soup.find_all('a'))
#输出:
#[<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.find(id = 'link3'))
#输出:<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a> for link in soup.find_all('a'):
print(link.get('href'))
#输出:
# http://example.com/elsie
# http://example.com/lacie
# http://example.com/tillie print(soup.getText())
#输出:
# The Dormouse's story
#
# The Dormouse's story
# Once upon a time there were three little sisters; and their names were
# Elsie,
# Lacie and
# Tillie;
# and they lived at the bottom of a well.
# ... print('all tags : <<<<<<')
for tag in soup.find_all(True):
print(tag.name)
#输出:
#html
#head
#title
#body
#p
#b
#p
#a
#a
#a
#p
Python BeautifulSoup4 使用指南的更多相关文章
- Python面向对象编程指南
		Python面向对象编程指南(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1SbD4gum4yGcUruH9icTPCQ 提取码:fzk5 复制这段内容后打开百度网 ... 
- Python 最佳实践指南 2018 学习笔记
		基础信息 版本 Python 2.7 Python 3.x Python2.7 版本在 2020 年后不再提供支持,建议新手使用 3.x 版本进行学习 实现 CPython:Python的标准实现: ... 
- PEP 8 - Python代码样式指南
		PEP 8 - Python代码样式指南 PEP: 8 标题: Python代码风格指南 作者: Guido van Rossum <python.org上的guido>,Barry Wa ... 
- Python 编码风格指南
		原文:http://python.jobbole.com/84618/ 本文超出 PEP8 的范畴以涵盖我认为优秀的 Python 风格.本文虽然坚持己见,却不偏执.不仅仅涉及语法.模块布局等问题,同 ... 
- PYTHON 最佳实践指南(转)
		add by zhj: 本文参考了The Hitchhiker's Guide to Python,当然也加入了作者的一些东西.The Hitchhiker's Guide to Python 的gi ... 
- (转)PEP 8——Python编码风格指南
		PEP 8——Python编码风格指南标签(空格分隔): Python PEP8 编码规范原文:https://lizhe2004.gitbooks.io/code-style-guideline-c ... 
- 机器学习实践:《Python机器学习实践指南》中文PDF+英文PDF+代码
		机器学习是近年来渐趋热门的一个领域,同时Python 语言经过一段时间的发展也已逐渐成为主流的编程语言之一.<Python机器学习实践指南>结合了机器学习和Python 语言两个热门的领域 ... 
- 学习推荐《从Excel到Python数据分析进阶指南》高清中文版PDF
		Excel是数据分析中最常用的工具,本书通过Python与Excel的功能对比介绍如何使用Python通过函数式编程完成Excel中的数据处理及分析工作.在Python中pandas库用于数据处理,我 ... 
- Python开发人员指南
		本指南是一个全面的资源贡献 给Python的 -为新的和经验丰富的贡献者.这是 保持由维护的Python同一社区.我们欢迎您对Python的贡献! 快速参考 这是设置和添加补丁所需的基本步骤.了解基础 ... 
随机推荐
- 完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法zt
			HTML5的语义化标签以及属性,可以让开发者非常方便地实现清晰的web页面布局,加上CSS3的效果渲染,快速建立丰富灵活的web页面显得非常简单. HTML5的新标签元素有: <header&g ... 
- 剑指offer 22 栈的压入、弹出序列
			class Solution { public: bool IsPopOrder(vector<int> pushV,vector<int> popV) { bool resu ... 
- 在iOS当中发送电子邮件和短信
			iOS实现发送电子邮件的方法很简单,首先导入MessageUI.framework框架,然后代码如下: #import "RPViewController.h" //添加邮件头文件 ... 
- Maven真——聚合和继承(于)
			依赖管理 我们谈论继承一个dependencies因素,我们非常easy这个特性被认为是适用于accout-parent于. 子模块account-email和account-persist同一时候依 ... 
- 我的小前端 (2)—— JQ和zepto
			没有什么特别新技术,就是记录我做移动端遇到的问题 2016-02-16 关于JS库 JQ很简单,网上很多插件效果都依赖它,但JQ库很大 zepto.js用简单效果,很好用 <script src ... 
- MS SQL:ID自增列从1开始重新排序
			数据库中把ID自增长重置成1: 一般做法:(太麻烦) 复制表数据->删除原表.新建一张表->粘贴: 新方法: 数据库中:新建查询->复制.粘贴一下代码->修改表名,执行即可(先 ... 
- win7(32 bit) 环境下点击打印预览报错解决办法
			如题,报错截图如下 : 解决办法如下: 1.关闭系统数据执行保护.具体操作: 在win7 系统命令行中执行如下命令: bcdedit.exe /set {current} nx Alw ... 
- GCD的使用和面试题集锦
			GCD 分为异步和同步 异步: dispatch_async ( 参数1 , { } 同步: dispatch_sync( 参数1 , { } 参数1 :队列 队列分为两种: dispatch_get ... 
- javascript函数apply和call
			apply:方法能劫持另外一个对象的方法,继承另外一个对象的属性. Function.apply(obj,args)方法能接收两个参数obj:这个对象将代替Function类里this对象args:这 ... 
- javaTemplates-学习笔记二
			配置PlayFramework环境 下载jar包[Play with Activator],这一步有点晕是JAVA程序员CMD执行的步骤;运行了jar包下载了很多配置文件什么的,有的资源没有VPN链接 ... 
