最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解BeautifulSoup獲取特定標(biāo)簽下內(nèi)容的方法

 更新時(shí)間:2020年12月07日 11:40:41   作者:qianc6350528  
這篇文章主要介紹了詳解BeautifulSoup獲取特定標(biāo)簽下內(nèi)容的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

以下是個(gè)人在學(xué)習(xí)beautifulSoup過(guò)程中的一些總結(jié),目前我在使用爬蟲數(shù)據(jù)時(shí)使用的方法的是:先用find_all()找出需要內(nèi)容所在的標(biāo)簽,如果所需內(nèi)容一個(gè)find_all()不能滿足,那就用兩個(gè)或者多個(gè)。接下來(lái)遍歷find_all的結(jié)果,用get_txt()、get(‘href')、得到文本或者鏈接,然后放入各自的列表中。這樣做有一個(gè)缺點(diǎn)就是txt的數(shù)據(jù)是一個(gè)單獨(dú)的列表,鏈接的數(shù)據(jù)也是一個(gè)單獨(dú)的列表,一方面不能體現(xiàn)這些數(shù)據(jù)之間的結(jié)構(gòu)性,另一方面當(dāng)想要獲得更多的內(nèi)容時(shí),就要?jiǎng)?chuàng)建更多的空列表。

遍歷所有標(biāo)簽:

soup.find_all('a')

找出所有頁(yè)面中含有標(biāo)簽a的html語(yǔ)句,結(jié)果以列表形式存儲(chǔ)。對(duì)找到的標(biāo)簽可以進(jìn)一步處理,如用for對(duì)結(jié)果遍歷,可以對(duì)結(jié)果進(jìn)行purify,得到如鏈接,字符等結(jié)果。

# 創(chuàng)建空列表
links=[] 
txts=[]
tags=soup.find_all('a')
for tag in tags:
  links.append(tag.get('href')
  txts.append(tag.txt)         #或者txts.append(tag.get_txt())

得到html的屬性名:

atr=[]
tags=soup.find_all('a')
for tag in tags:
  atr.append(tag.p('class')) # 得到a 標(biāo)簽下,子標(biāo)簽p的class名稱 

find_all()的相關(guān)用法實(shí)例:

實(shí)例來(lái)自BeautifulSoup中文文檔

1. 字符串

最簡(jiǎn)單的過(guò)濾器是字符串.在搜索方法中傳入一個(gè)字符串參數(shù),Beautiful Soup會(huì)查找與字符串完整匹配的內(nèi)容,下面的例子用于查找文檔中所有的標(biāo)簽:

soup.find_all('b')
# [<b>The Dormouse's story</b>]

2.正則表達(dá)式

如果傳入正則表達(dá)式作為參數(shù),Beautiful Soup會(huì)通過(guò)正則表達(dá)式的 match() 來(lái)匹配內(nèi)容.下面例子中找出所有以b開(kāi)頭的標(biāo)簽,這表示和標(biāo)簽都應(yīng)該被找到:

import re
for tag in soup.find_all(re.compile("^b")):
  print(tag.name)
# body
# b

下面代碼找出所有名字中包含”t”的標(biāo)簽:

for tag in soup.find_all(re.compile("t")):
  print(tag.name)
# html
# title

3.列表

如果傳入列表參數(shù),Beautiful Soup會(huì)將與列表中任一元素匹配的內(nèi)容返回.下面代碼找到文檔中所有標(biāo)簽和標(biāo)簽:

soup.find_all(["a", "b"])
# [<b>The Dormouse's story</b>,
# <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>,
# <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
# <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

4.方法(自定義函數(shù),傳入find_all)

如果沒(méi)有合適過(guò)濾器,那么還可以定義一個(gè)方法,方法只接受一個(gè)元素參數(shù) [4] ,如果這個(gè)方法返回 True 表示當(dāng)前元素匹配并且被找到,如果不是則反回 False
下面方法校驗(yàn)了當(dāng)前元素,如果包含 class 屬性卻不包含 id 屬性,那么將返回 True:

def has_class_but_no_id(tag):
  return tag.has_attr('class') and not tag.has_attr('id')```

返回結(jié)果中只有

標(biāo)簽沒(méi)有標(biāo)簽,因?yàn)闃?biāo)簽還定義了”id”,沒(méi)有返回和,因?yàn)楹椭袥](méi)有定義”class”屬性.
下面代碼找到所有被文字包含的節(jié)點(diǎn)內(nèi)容:

from bs4 import NavigableString
def surrounded_by_strings(tag):
  return (isinstance(tag.next_element, NavigableString)
      and isinstance(tag.previous_element, NavigableString))

for tag in soup.find_all(surrounded_by_strings):
  print tag.name
# p
# a
# a
# a
# p

5.按照CSS搜索

按照CSS類名搜索tag的功能非常實(shí)用,但標(biāo)識(shí)CSS類名的關(guān)鍵字 class 在Python中是保留字,使用 class 做參數(shù)會(huì)導(dǎo)致語(yǔ)法錯(cuò)誤.從Beautiful Soup的4.1.1版本開(kāi)始,可以通過(guò) class_ 參數(shù)搜索有指定CSS類名的tag:

soup.find_all("a", class_="sister")
# [<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>,
# <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
# <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

或者:

soup.find_all("a", attrs={"class": "sister"})
# [<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>,
# <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>,
# <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

6.按照text參數(shù)查找

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

soup.find_all(text="Elsie")
# [u'Elsie']

soup.find_all(text=["Tillie", "Elsie", "Lacie"])
# [u'Elsie', u'Lacie', u'Tillie']

soup.find_all(text=re.compile("Dormouse"))
[u"The Dormouse's story", u"The Dormouse's story"]

def is_the_only_string_within_a_tag(s):
  ""Return True if this string is the only child of its parent tag.""
  return (s == s.parent.string)

soup.find_all(text=is_the_only_string_within_a_tag)
# [u"The Dormouse's story", u"The Dormouse's story", u'Elsie', u'Lacie', u'Tillie', u'...']

雖然 text 參數(shù)用于搜索字符串,還可以與其它參數(shù)混合使用來(lái)過(guò)濾tag.Beautiful Soup會(huì)找到 .string 方法與 text 參數(shù)值相符的tag.下面代碼用來(lái)搜索內(nèi)容里面包含“Elsie”的標(biāo)簽:

soup.find_all("a", text="Elsie")
# [<a  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1">Elsie</a>]

7.只查找當(dāng)前標(biāo)簽的子節(jié)點(diǎn)

調(diào)用tag的 find_all() 方法時(shí),Beautiful Soup會(huì)檢索當(dāng)前tag的所有子孫節(jié)點(diǎn),如果只想搜索tag的直接子節(jié)點(diǎn),可以使用參數(shù) recursive=False .

一段簡(jiǎn)單的文檔:

<html>
 <head>
 <title>
  The Dormouse's story
 </title>
 </head>
...

是否使用 recursive 參數(shù)的搜索結(jié)果:

soup.html.find_all("title")
# [<title>The Dormouse's story</title>]

soup.html.find_all("title", recursive=False)
# []

到此這篇關(guān)于詳解BeautifulSoup獲取特定標(biāo)簽下內(nèi)容的方法的文章就介紹到這了,更多相關(guān)BeautifulSoup獲取特定標(biāo)簽內(nèi)容內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于Python制作三款起床鬧鐘的示例代碼

    基于Python制作三款起床鬧鐘的示例代碼

    每天上班最痛苦的事情就是早起早起早起!這是大部分上班族的痛苦,但是不上班又是不可能的啦,因?yàn)槎际菫榱烁沐X。本文用Python制作了三款有趣的鬧鐘,感興趣的可以學(xué)習(xí)一下
    2022-05-05
  • python實(shí)現(xiàn)線性回歸的示例代碼

    python實(shí)現(xiàn)線性回歸的示例代碼

    線性回歸就是通過(guò)多次取點(diǎn),找出符合函數(shù)的曲線,那么就可以完成一維線性回歸,本文通過(guò)實(shí)例代碼給大家介紹python實(shí)現(xiàn)線性回歸的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2022-02-02
  • python字典按照value排序方法

    python字典按照value排序方法

    在本篇文章里小編給各位分享一篇關(guān)于python字典按照value排序方法的相關(guān)文章,有興趣的朋友們可以學(xué)習(xí)下。
    2020-12-12
  • 使用turtle繪制五角星、分形樹(shù)

    使用turtle繪制五角星、分形樹(shù)

    這篇文章主要為大家詳細(xì)介紹了使用turtle繪制五角星、分形樹(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • 詳細(xì)介紹Python中的set集合

    詳細(xì)介紹Python中的set集合

    本文詳細(xì)介紹了Python中set集合的基本概念和詳細(xì)用法,希望對(duì)讀者朋友們有所幫助。需要的朋友可以參考下面具體的文章內(nèi)容
    2021-09-09
  • 關(guān)于Java中RabbitMQ的高級(jí)特性

    關(guān)于Java中RabbitMQ的高級(jí)特性

    這篇文章主要介紹了關(guān)于Java中RabbitMQ的高級(jí)特性,MQ全稱為Message Queue,即消息隊(duì)列,"消息隊(duì)列"是在消息的傳輸過(guò)程中保存消息的容器,它是典型的:生產(chǎn)者、消費(fèi)者模型,生產(chǎn)者不斷向消息隊(duì)列中生產(chǎn)消息,消費(fèi)者不斷的從隊(duì)列中獲取消息,需要的朋友可以參考下
    2023-07-07
  • 利用Python代碼制作過(guò)年春聯(lián)

    利用Python代碼制作過(guò)年春聯(lián)

    這篇文章主要介紹了如何利用代碼編寫過(guò)年的春聯(lián),文中一共介紹了兩種方法,一是利用HTML+CSS+JS,二是利用Python,感興趣的可以試一試
    2022-01-01
  • Python OpenCV中的圖像處理物體跟蹤效果

    Python OpenCV中的圖像處理物體跟蹤效果

    我們知道怎樣將一幅圖像從 BGR 轉(zhuǎn)換到 HSV 了,我們可以利用這一點(diǎn)來(lái)提取帶有某個(gè)特定顏色的物體,這篇文章主要介紹了Python OpenCV中的圖像處理物體跟蹤,需要的朋友可以參考下
    2023-08-08
  • Python開(kāi)發(fā)之pip安裝及使用方法詳解

    Python開(kāi)發(fā)之pip安裝及使用方法詳解

    這篇文章主要介紹了Python開(kāi)發(fā)之pip安裝及使用方法詳解,需要的朋友可以參考下
    2020-02-02
  • python ansible自動(dòng)化運(yùn)維工具執(zhí)行流程

    python ansible自動(dòng)化運(yùn)維工具執(zhí)行流程

    ansible是基于 paramiko 開(kāi)發(fā)的,并且基于模塊化工作,本身沒(méi)有批量部署的能力,接下來(lái)通過(guò)本文給大家分享python ansible自動(dòng)化運(yùn)維工具的特點(diǎn)及執(zhí)行流程,感興趣的朋友跟隨小編一起看看吧
    2021-06-06

最新評(píng)論

县级市| 东丰县| 井陉县| 黄浦区| 兴安县| 云龙县| 黄山市| 福海县| 永修县| 深水埗区| 班玛县| 汝阳县| 天祝| 西林县| 涿鹿县| 万载县| 安塞县| 竹溪县| 县级市| 剑河县| 镇原县| 水城县| 乌兰县| 西城区| 靖州| 健康| 买车| 泸水县| 新宁县| 神农架林区| 汝南县| 吉安县| 东宁县| 祥云县| 宜昌市| 定州市| 道孚县| 宝丰县| 丰都县| 平遥县| 镇江市|