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的贡献! 快速参考 这是设置和添加补丁所需的基本步骤.了解基础 ...
随机推荐
- hdu 5730 Shell Necklace fft+cdq分治
题目链接 dp[n] = sigma(a[i]*dp[n-i]), 给出a1.....an, 求dp[n]. n为1e5. 这个式子的形式显然是一个卷积, 所以可以用fft来优化一下, 但是这样也是会 ...
- hdu 5724 Chess 博弈
题目链接 一个n行20列的棋盘. 每一行有若干个棋子. 两人轮流操作, 每人每次可以将一个棋子向右移动一个位置, 如果它右边有一个棋子, 就跳过这个棋子, 如果有若干个棋子, 就将这若干个都跳过. 但 ...
- 51cto那些技术专题们
Nginx配置与应用详解 UML(Unified Modeling Language,统一建模语言) 架构师的成长历程 python python book ruby html5 不可不知的Linux ...
- scanf和gets的区别
scanf和gets获取字符串时的区别 在C语言中,能构获取字符串的函数至少有两个: 1.scanf() 所在头文件:stdio.h 语法:scanf("格式控制字符串" ...
- Xcode 真机测试破解方法(转加修改)xcode 4.3 通过
Xcode 真机测试破解方法(转加修改)xcode 4.3 通过 生成本机证书 应用程序->实用工具->钥匙串访问 菜单:钥匙串访问->证书助理->创建证书, 然后按以下图片顺 ...
- chrome 下的 proxy 插件安装
Install “Proxy SwitchyOmega” extensions for chrome.
- 无废话ubuntu 13.4w文件共享配置
目标:实现windows和linux混合组成的操作 系统中可以共享文件,并可以通过机器名互相访问 安装文件共享服务 0.更改本机主机名,修改 /etc/hostname文件和/etc/hosts文件中 ...
- EF Dal通用类
一个通用的ef dal处理类是非擦汗那个提高工作效率的 using System; using System.Collections.Generic; using System.Data.Enti ...
- JavaScript之获取和设置元素属性
1.与我前面的随笔获取元素的那些方法不同http://www.cnblogs.com/GreenLeaves/p/5689075.html 获取元素属性的方法getAttribute()不属于docu ...
- IE 调试JS加断点不管用 增加debugger
ie按F12添加断点调试js不管用 有个方法在你想加断点前面加debugger,再调试就管用了