首页 > 代码库 > Python开发【模块】:BeautifulSoup

Python开发【模块】:BeautifulSoup

BeautifulSoup

BeautifulSoup是一个模块,该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后遍可以使用他提供的方法进行快速查找指定元素,从而使得在HTML或XML中查找指定元素变得简单

1、安装:

pip3 install beautifulsoup4
pip install  lxml   # python2.x

 

2、简单使用:

from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse‘s story</title></head>
<body>
asdf
    <div class="title">
        <b>The Dormouse‘s story总共</b>
        <h1>f</h1>
    </div>
<div class="story">Once upon a time there were three little sisters; and their names were
    <a  class="sister0" id="link1">Els<span>f</span>ie</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.</div>
ad<br/>sf
<p class="story">...</p>
</body>
</html>
"""

# soup = BeautifulSoup(html_doc, features="lxml")
soup = BeautifulSoup(html_doc, features="html.parser") # 等同于上面
# 找到第一个a标签
tag1 = soup.find(name=‘a‘)
# 找到所有的a标签
tag2 = soup.find_all(name=‘a‘)
# 找到id=link2的标签
tag3 = soup.select(‘#link2‘)

print(tag1)
print(tag2)
print(tag3)
# <a class="sister0" id="link1">Els<span>f</span>ie</a>
# [<a class="sister0" id="link1">Els<span>f</span>ie</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>]
# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

 

3、标签方法:

name标签名称

# name
tag = soup.find(‘a‘)
# <a class="sister0" id="link1">Els<span>f</span>ie</a>
name = tag.name # 获取
print(name)  # a
tag.name = ‘span‘ # 设置 替换第一个a标签为span标签
print(soup)

attrs标签属性

# attrs
tag = soup.find(‘a‘)
# <a class="sister0" id="link1">Els<span>f</span>ie</a>
attrs = tag.attrs          # 获取
print(attrs)
# {‘class‘: [‘sister0‘], ‘id‘: ‘link1‘}
tag.attrs = {‘ik‘:123}     # 设置属性
tag.attrs[‘id‘] = ‘iiiii‘ # 更改id
print(soup)

 children所有子标签

# 子标签,只是获取儿子
tag = soup.find(‘a‘)
# <a class="sister0" id="link1">Els<span>f</span>ie</a>
v = tag.children
print(v)
# <list_iterator object at 0x02E71230>
for i in v:
    print(i)
# Els
# <span>f</span>
# ie

 descendants子子孙孙标签

# 获得子子孙孙
body = soup.find(‘body‘)
v = body.descendants
print(v)
for i in v:
    print(i)

clear将标签的所有子标签全部清空(保留标签名)  

# 清空表签内容
tag = soup.find(‘body‘)
tag.clear()
print(soup)

# <html><head><title>The Dormouse‘s story</title></head>
# <body></body>
# </html>

 decompose,递归的删除所有的标签

# 递归的删除所有的标签
body = soup.find(‘body‘)
body.decompose()
print(soup)

# <html><head><title>The Dormouse‘s story</title></head>
# 
# </html>

 extract递归的删除所有的标签,并获取删除的标签

# 递归的删除所有的标签,并获取删除的标签
body = soup.find(‘body‘)
v = body.extract()
print(soup)
# <html><head><title>The Dormouse‘s story</title></head>
#
# </html>
print(v,type(v))
# <body>
# asdf
#     <div class="title">
# <b>The Dormouse‘s story总共</b>
# <h1>f</h1>
# </div>
# <div id="story">Once upon a time there were three little sisters; and their names were
#     <a class="sister0" id="link1">Els<span>f</span>ie</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.</div>
# ad<br/>sf
# <p class="story">...</p>
# </body> <class ‘bs4.element.Tag‘>

  

 

 

 

 

 

 

Python开发【模块】:BeautifulSoup