前言:

昨天把传说中的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 使用指南的更多相关文章

  1. Python面向对象编程指南

    Python面向对象编程指南(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1SbD4gum4yGcUruH9icTPCQ 提取码:fzk5 复制这段内容后打开百度网 ...

  2. Python 最佳实践指南 2018 学习笔记

    基础信息 版本 Python 2.7 Python 3.x Python2.7 版本在 2020 年后不再提供支持,建议新手使用 3.x 版本进行学习 实现 CPython:Python的标准实现: ...

  3. PEP 8 - Python代码样式指南

    PEP 8 - Python代码样式指南 PEP: 8 标题: Python代码风格指南 作者: Guido van Rossum <python.org上的guido>,Barry Wa ...

  4. Python 编码风格指南

    原文:http://python.jobbole.com/84618/ 本文超出 PEP8 的范畴以涵盖我认为优秀的 Python 风格.本文虽然坚持己见,却不偏执.不仅仅涉及语法.模块布局等问题,同 ...

  5. PYTHON 最佳实践指南(转)

    add by zhj: 本文参考了The Hitchhiker's Guide to Python,当然也加入了作者的一些东西.The Hitchhiker's Guide to Python 的gi ...

  6. (转)PEP 8——Python编码风格指南

    PEP 8——Python编码风格指南标签(空格分隔): Python PEP8 编码规范原文:https://lizhe2004.gitbooks.io/code-style-guideline-c ...

  7. 机器学习实践:《Python机器学习实践指南》中文PDF+英文PDF+代码

    机器学习是近年来渐趋热门的一个领域,同时Python 语言经过一段时间的发展也已逐渐成为主流的编程语言之一.<Python机器学习实践指南>结合了机器学习和Python 语言两个热门的领域 ...

  8. 学习推荐《从Excel到Python数据分析进阶指南》高清中文版PDF

    Excel是数据分析中最常用的工具,本书通过Python与Excel的功能对比介绍如何使用Python通过函数式编程完成Excel中的数据处理及分析工作.在Python中pandas库用于数据处理,我 ...

  9. Python开发人员指南

    本指南是一个全面的资源贡献 给Python的 -为新的和经验丰富的贡献者.这是 保持由维护的Python同一社区.我们欢迎您对Python的贡献! 快速参考 这是设置和添加补丁所需的基本步骤.了解基础 ...

随机推荐

  1. scanf和gets的区别

    scanf和gets获取字符串时的区别  在C语言中,能构获取字符串的函数至少有两个:  1.scanf()   所在头文件:stdio.h  语法:scanf("格式控制字符串" ...

  2. c/c++实现混合编程

    在开发中大家经常会使用到c与c++混合编程,这样能够更好的实现功能模块.刚学习了一下c和c++的混合编程,参考了网上的相关知识,在这里留下要点,方便以后进行查阅. 1.extern关键字 extern ...

  3. [摘]ASP.Net标准控件(TextBox控件)

    TextBox控件 TextBox控件又称文本框控件,为用户提供输入文本的功能. 1.属性 TextBox控件的常用属性及说明如表1所示. 表1 TextBox控件常用属性及说明 属    性 说   ...

  4. Silverlight下的Socket通讯

    http://www.cnblogs.com/chengxingliang/archive/2012/05/24/2515100.html 在Silverlight中进行通讯,只能使用4502-453 ...

  5. dllimport与dllexport作用与区别

    我相信写WIN32程序的人,做过DLL,都会很清楚__declspec(dllexport)的作用,它就是为了省掉在DEF文件中手工定义导出哪些函数的一个方法.当然,如果你的DLL里全是C++的类的话 ...

  6. Ubuntu14.04 Server Apache2+subversion环境搭建

    自从工作后,发现之前的代码开发太随便啦,于是经过不到两年的工作积累,打算在自己开发软件的过程中好好管理自己的项目.于是打算搭建自己的项目服务器,去年搭建过一次,但是由于没有记录,现在需要再来一遍,好多 ...

  7. 【Deep Learning】genCNN: A Convolutional Architecture for Word Sequence Prediction

    作者:Mingxuan Wang.李航,刘群 单位:华为.中科院 时间:2015 发表于:acl 2015 文章下载:http://pan.baidu.com/s/1bnBBVuJ 主要内容: 用de ...

  8. iOS-BLE蓝牙开发

    Demo地址:WEBlueToothManager 在写这个博客之前,空余时间抽看了近一个月的文档和Demo,系统给的解释很详细,接口也比较实用,唯独有一点,对于设备 的唯一标示,网上众说纷纭,在这里 ...

  9. iOS 使用Method Swizzling隐藏Status Bar

    在iOS 6中,隐藏Status Bar很的简单. // iOS 6及曾经,隐藏状态栏 [[UIApplication sharedApplication] setStatusBarHidden:YE ...

  10. 翻转View

    翻转View by 伍雪颖 CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil cont ...