久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

Python爬蟲中搜索文檔樹的方法-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!

成都創(chuàng)新互聯(lián)公司服務(wù)項目包括南漳網(wǎng)站建設(shè)、南漳網(wǎng)站制作、南漳網(wǎng)頁制作以及南漳網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,南漳網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到南漳省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(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.傳正則表達式

如果傳入正則表達式作為參數(shù),Beautiful Soup會通過正則表達式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ù)接受字符串,正則表達式,列表。

#!/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"]))
 
# 正則表達式
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é)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

新聞名稱:Python爬蟲中搜索文檔樹的方法-創(chuàng)新互聯(lián)
當(dāng)前地址:http://sd-ha.com/article6/dcecog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、做網(wǎng)站、標(biāo)簽優(yōu)化網(wǎng)站建設(shè)、小程序開發(fā)、微信公眾號

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)