溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python爬蟲中搜索文檔樹的方法

發(fā)布時間:2020-08-07 11:41:38 來源:億速云 閱讀:111 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)Python爬蟲中搜索文檔樹的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

搜索文檔樹

1.find_all(name, attrs, recursive, text, **kwargs)

1)name參數(shù)

name參數(shù)可以查找所有名字為name的Tag,字符串對象會被自動忽略掉。

a.傳字符串

最簡單的過濾器就是字符串,在搜索方法中傳入一個字符串參數(shù),Beautiful Soup會查找與字符串完整匹配所有的內(nèi)容,返回一個列表。

#!/usr/bin/python3
# -*- coding:utf-8 -*-
 
from bs4 import BeautifulSoup
 
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>
"""
 
# 創(chuàng)建 Beautiful Soup 對象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")
 
print(soup.find_all("b")) 
print(soup.find_all("a"))

運行結(jié)果

[<b>The Dormouse's story</b>]
[<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>]

B.傳正則表達(dá)式

如果傳入正則表達(dá)式作為參數(shù),Beautiful Soup會通過正則表達(dá)式match()來匹配內(nèi)容。

#!/usr/bin/python3
# -*- coding:utf-8 -*-
 
from bs4 import BeautifulSoup
import re
 
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>
"""
 
# 創(chuàng)建 Beautiful Soup 對象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")
 
for tag in soup.find_all(re.compile("^b")):
    print(tag.name)

運行結(jié)果

body
b

C.傳列表

如果傳入列表參數(shù),Beautiful Soup會將與列表中任一元素匹配的內(nèi)容以列表方式返回。

#!/usr/bin/python3
# -*- coding:utf-8 -*-
 
from bs4 import BeautifulSoup
 
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>
"""
 
# 創(chuàng)建 Beautiful Soup 對象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")
 
print(soup.find_all(['a', 'b']))

2)keyword參數(shù)

#!/usr/bin/python3
# -*- coding:utf-8 -*-
 
from bs4 import BeautifulSoup
 
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>
"""
 
# 創(chuàng)建 Beautiful Soup 對象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")
 
print(soup.find_all(id="link1"))

運行結(jié)果

[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]

3)text參數(shù)

通過text參數(shù)可以搜索文檔中的字符串內(nèi)容,與name參數(shù)的可選值一樣,text參數(shù)接受字符串,正則表達(dá)式,列表。

#!/usr/bin/python3
# -*- coding:utf-8 -*-
 
from bs4 import BeautifulSoup
import re
 
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>
"""
 
# 創(chuàng)建 Beautiful Soup 對象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")
 
# 字符串
print(soup.find_all(text = " Elsie "))
 
# 列表
print(soup.find_all(text = ["Tillie", " Elsie ", "Lacie"]))
 
# 正則表達(dá)式
print(soup.find_all(text = re.compile("Dormouse")))

運行結(jié)果

[' Elsie ']
[' Elsie ', 'Lacie', 'Tillie']
["The Dormouse's story", "The Dormouse's story"]

關(guān)于Python爬蟲中搜索文檔樹的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI