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>
"""
bs = BeautifulSoup(html_doc,'lxml')# 通过标签查找print(bs.select('b'))# [<b>The Dormouse's story</b>]# 通过类名查找print(bs.select('.title'))# [<p class="title"><b>The Dormouse's story</b></p>]# 通过id名来查找print(bs.select('#link1'))# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
修改文档树(不常用)
修改tag的名称和属性
修改string属性值,相当于用当前的内容替代原来的内容
append()方法,向tag中添加内容(类似列表的append()方法)
decompose()方法,修改删除段落
修改tag名称和属性
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>
"""
bs = BeautifulSoup(html_doc,'lxml')print(bs.p)# <p class="title"><b>The Dormouse's story</b></p># 修改tag名称
bs.p.name ='z'print(bs.z)# <z class="title"><b>The Dormouse's story</b></z># 修改属性
bs.z['class']='name'print(bs.z)# <z class="name"><b>The Dormouse's story</b></z>
修改string属性值
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>
"""
bs = BeautifulSoup(html_doc,'lxml')print(bs.title)# <title>The Dormouse's story</title># 修改string属性值
bs.title.string ='Book Name'print(bs.title)# <title>Book Name</title>
append()方法
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>
"""
bs = BeautifulSoup(html_doc,'lxml')print(bs.p)# <p class="title"><b>The Dormouse's story</b></p># append()方法
bs.p.append('abc')# <p class="name"><b>The Dormouse's story</b>abc</p>
decompose()方法
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>
"""
bs = BeautifulSoup(html_doc,'lxml')print(bs.p)# <p class="title"><b>The Dormouse's story</b></p># decompose()方法
bs.b.decompose()print(bs.p)# <p class="name"></p>