beautiful soup 解析库的使用也是相当简单的。
from bs4 import BeautifulSoup
soup = BeautifulSoup('<p>Hello</p>', 'lxml')
# Hello
print(soup.p.string)
安装:
如要使用beautiful soup 这个解析库,请确保已经安装了 beautiful soup 和 lxml 这两个库。
pip install beautifulsoup4
pip install lxml
基本使用:
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><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>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print(soup.prettify())
print(soup.title.string)
节点选择器:
直接调用节点的名称即可选择节点, 然后调用string方法就可以得到节点内的文本内容了,
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><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>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print(soup.title)
print(type(soup.title))
print(soup.title.string)
print(soup.head)
print(soup.p)
获取名称:
使用name 属性可以获取节点的名称
print(soup.title.name)
获取属性:
一个节点可以拥有多个属性, 例如, id, class, name 等, 选择这个节点元素后, 可以调用 attrs 属性来获取其所有的属性值:
print(soup.attrs)
print(soup.attrs['name'])
还有一个更为简单的方式, 就是直接在选择的节点后加中括号,然后传入要选择的属性即可
print(soup.p['class'])
关联选择:
有时候, 我们不能一步就选择到想要的节点, 例如:我们先选择一个节点,然后以此节点为基础的情况下, 选择他的子节点,父节点,兄弟节点等。
子节点和子孙节点:
当我们选择一个节点后,如果想要获取它的直接子节点,则需要调用contents 属性:
html = """
<html>
<head>
<title>The Dormouse's story</title>
</head>
<body>
<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">
<span>Elsie</span>
</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>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print(soup.p.contents)
可以发现, 返回的是一个列表,这个列表中,既包括了节点信息。
同样, 可以调用children 属性来获取相应的结果:
html = """
<html>
<head>
<title>The Dormouse's story</title>
</head>
<body>
<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">
<span>Elsie</span>
</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>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print(soup.p.children)
for i, child in enumerate(soup.p.children):
print(i, child)
如果想要得到所有的子孙节点, 可以调用 descendants 属性
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print(soup.p.descendants)
for i, child in enumerate(soup.p.descendants):
print(i, child)
父节点和祖先节点:
获取某个节点的父节点,则需要调用parent 属性
html = """
<html>
<head>
<title>The Dormouse's story</title>
</head>
<body>
<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">
<span>Elsie</span>
</a>
</p>
<p class="story">...</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
print(soup.a.parent)
需要注意的是, 这里获取的是 a 节点的直接父节点, 如果想继续向上获取, 例如, 获取祖先节点,则需要调用 parents 属性