Python BeautifulSoup4 使用指南

来源:互联网 发布:excel多个矩阵相乘 编辑:程序博客网 时间:2024/06/06 04:34

前言:

 

昨天把传说中的BeautifulSoup4装上了,还没有装好的童鞋,请看本人的上一篇博客:

Python3 Win7安装 BeautifulSoup,按照里面简单的步骤就可以把BeautifulSoup装上啦,很简单的,表害怕

 

装好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)#输出:titleprint(soup.title.parent.name)#输出:headprint(soup.title.parent.parent.name)#输出:htmlprint(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/tillieprint(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






1 0
原创粉丝点击