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






原文地址:https://www.cnblogs.com/blfshiye/p/5139424.html