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

BeautifulSoup中find和find_all的使用詳解

 更新時(shí)間:2020年12月07日 10:13:25   作者:OCISLU  
這篇文章主要介紹了BeautifulSoup中find和find_all的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

爬蟲利器BeautifulSoup中find和find_all的使用方法

二話不說,先上段HTML例子

<html>
  <head>
    <title>
      index
    </title>
  </head>
  <body>
     <div>
        <ul>
           <li id="flask"class="item-0"><a href="link1.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >first item</a></li>
          <li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li>
          <li class="item-inactie"><a href="link3.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >third item</a></li>
          <li class="item-1"><a href="link4.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fourth item</a></li>
          <li class="item-0"><a href="link5.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fifth item</a>
         </ul>
     </div>
    <li> hello world </li>
  </body>
</html>

使用BeautifulSoup前需要先構(gòu)建BeautifulSoup實(shí)例

# 構(gòu)建beautifulsoup實(shí)例
soup = BeautifulSoup(html,'lxml')
# 第一個(gè)參數(shù)是要匹配的內(nèi)容
# 第二個(gè)參數(shù)是beautifulsoup要采用的模塊,即規(guī)則

需要注意的是,導(dǎo)入對(duì)的模塊需要事先安裝,此處導(dǎo)入的LXML事先已經(jīng)安裝。可以導(dǎo)入的模塊可通過查詢BeautifulSoup的文檔查看

第一次插入圖片,那,我表個(gè)白,我超愛我女朋友呼延羿彤~~

接下來(lái)是find和find_all的介紹

1. find
只返回第一個(gè)匹配到的對(duì)象
語(yǔ)法:

find(name, attrs, recursive, text, **wargs)    
# recursive 遞歸的,循環(huán)的

BeautifulSoup的find方法

參數(shù):

參數(shù)名 作用
name 查找標(biāo)簽
text 查找文本
attrs 基于attrs參數(shù)

例子:

# find查找一次
li = soup.find('li')
print('find_li:',li)
print('li.text(返回標(biāo)簽的內(nèi)容):',li.text)
print('li.attrs(返回標(biāo)簽的屬性):',li.attrs)
print('li.string(返回標(biāo)簽內(nèi)容為字符串):',li.string)

運(yùn)行結(jié)果:

find_li: <li class="item-0" id="flask"><a href="link1.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >first item</a></li>
li.text(返回標(biāo)簽的內(nèi)容): first item
li.attrs(返回標(biāo)簽的屬性): {'id': 'flask', 'class': ['item-0']}
li.string(返回標(biāo)簽內(nèi)容為字符串): first item

find也可以通過‘屬性=值'的方法進(jìn)行匹配

li = soup.find(id = 'flask')
print(li,'\n')
<li class="item-0" id="flask"><a href="link1.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >first item</a></li> 

需要注意的是,因?yàn)閏lass是python的保留關(guān)鍵字,若要匹配標(biāo)簽內(nèi)class的屬性,需要特殊的方法,有以下兩種:

  • 在attrs屬性用字典的方式進(jìn)行參數(shù)傳遞
  • BeautifulSoup自帶的特別關(guān)鍵字class_
# 第一種:在attrs屬性用字典進(jìn)行傳遞參數(shù)
find_class = soup.find(attrs={'class':'item-1'})
print('findclass:',find_class,'\n')
# 第二種:BeautifulSoup中的特別關(guān)鍵字參數(shù)class_
beautifulsoup_class_ = soup.find(class_ = 'item-1')
print('BeautifulSoup_class_:',beautifulsoup_class_,'\n')

運(yùn)行結(jié)果

findclass: <li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li>

BeautifulSoup_class_: <li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li>

2. find_all

返回所有匹配到的結(jié)果,區(qū)別于find(find只返回查找到的第一個(gè)結(jié)果)

語(yǔ)法:

find_all(name, attrs, recursive, text, limit, **kwargs)

BeautifulSoup的find_all方法

參數(shù)名 作用
name 查找標(biāo)簽
text 查找文本
attrs 基于attrs參數(shù)

與find一樣的語(yǔ)法

上代碼

# find_all 查找所有
li_all = soup.find_all('li')
for li_all in li_all:
	print('---')
	print('匹配到的li:',li_all)
	print('li的內(nèi)容:',li_all.text)
	print('li的屬性:',li_all.attrs)

運(yùn)行結(jié)果:

---
匹配到的li: <li class="item-0" id="flask"><a href="link1.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >first item</a></li>
li的內(nèi)容: first item
li的屬性: {'id': 'flask', 'class': ['item-0']}
---
匹配到的li: <li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li>
li的內(nèi)容: second item
li的屬性: {'class': ['item-1']}
---
匹配到的li: <li cvlass="item-inactie"><a href="link3.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >third item</a></li>
li的內(nèi)容: third item
li的屬性: {'cvlass': 'item-inactie'}
---
匹配到的li: <li class="item-1"><a href="link4.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fourth item</a></li>
li的內(nèi)容: fourth item
li的屬性: {'class': ['item-1']}
---
匹配到的li: <li class="item-0"><a href="link5.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fifth item</a>
</li>
li的內(nèi)容: fifth item

附上比較靈活的find_all查詢方法:

# 最靈活的使用方式
li_quick = soup.find_all(attrs={'class':'item-1'})
for li_quick in li_quick:
	print('最靈活的查找方法:',li_quick)

運(yùn)行結(jié)果:

  • 最靈活的查找方法: <li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li>
  • 最靈活的查找方法: <li class="item-1"><a href="link4.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fourth item</a></li>

完整代碼:

# coding=utf8
# @Author= CaiJunxuan
# @QQ=469590490
# @Wechat:15916454524

# beautifulsoup

# 導(dǎo)入beautifulsoup模塊
from bs4 import BeautifulSoup

# HTML例子
html = '''
<html>
  <head>
    <title>
      index
    </title>
  </head>
  <body>
     <div>
        <ul>
           <li id="flask"class="item-0"><a href="link1.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >first item</a></li>
          <li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li>
          <li cvlass="item-inactie"><a href="link3.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >third item</a></li>
          <li class="item-1"><a href="link4.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fourth item</a></li>
          <li class="item-0"><a href="link5.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fifth item</a>
         </ul>
     </div>
    <li> hello world </li>
  </body>
</html>
'''

# 構(gòu)建beautifulsoup實(shí)例
soup = BeautifulSoup(html,'lxml')
# 第一個(gè)參數(shù)是要匹配的內(nèi)容
# 第二個(gè)參數(shù)是beautifulsoup要采用的模塊,即規(guī)則
# html.parser是python內(nèi)置的結(jié)構(gòu)匹配方法,但是效率不如lxml所以不常用
# lxml 采用lxml模塊
# html5lib,該模塊可以將內(nèi)容轉(zhuǎn)換成html5對(duì)象
# 若想要以上功能,就需要具備對(duì)應(yīng)的模塊,比如使用lxml就要安裝lxml

# 在bs4當(dāng)中有很多種匹配方法,但常用有兩種:

# find查找一次
li = soup.find('li')
print('find_li:',li)
print('li.text(返回標(biāo)簽的內(nèi)容):',li.text)
print('li.attrs(返回標(biāo)簽的屬性):',li.attrs)
print('li.string(返回標(biāo)簽內(nèi)容為字符串):',li.string)
print(50*'*','\n')

# find可以通過'屬性 = 值'的方法進(jìn)行select
li = soup.find(id = 'flask')
print(li,'\n')
# 因?yàn)閏lass是python的保留關(guān)鍵字,所以無(wú)法直接查找class這個(gè)關(guān)鍵字
# 有兩種方法可以進(jìn)行class屬性查詢
# 第一種:在attrs屬性用字典進(jìn)行傳遞參數(shù)
find_class = soup.find(attrs={'class':'item-1'})
print('findclass:',find_class,'\n')
# 第二種:BeautifulSoup中的特別關(guān)鍵字參數(shù)class_
beautifulsoup_class_ = soup.find(class_ = 'item-1')
print('BeautifulSoup_class_:',beautifulsoup_class_,'\n')

# find_all 查找所有
li_all = soup.find_all('li')
for li_all in li_all:
	print('---')
	print('匹配到的li:',li_all)
	print('li的內(nèi)容:',li_all.text)
	print('li的屬性:',li_all.attrs)

# 最靈活的使用方式
li_quick = soup.find_all(attrs={'class':'item-1'})
for li_quick in li_quick:
	print('最靈活的查找方法:',li_quick)

到此這篇關(guān)于BeautifulSoup中find和find_all的使用詳解的文章就介紹到這了,更多相關(guān)BeautifulSoup find和find_all內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python中values()函數(shù)用法簡(jiǎn)單示例

    Python中values()函數(shù)用法簡(jiǎn)單示例

    這篇文章主要給大家介紹了關(guān)于Python中values()函數(shù)用法的相關(guān)資料,python內(nèi)置的values()函數(shù)返回一個(gè)字典中所有的值,文中給出了代碼示例,需要的朋友可以參考下
    2023-09-09
  • Python編程使用Selenium模擬淘寶登錄實(shí)現(xiàn)過程

    Python編程使用Selenium模擬淘寶登錄實(shí)現(xiàn)過程

    這篇文章主要介紹了Python編程使用Selenium模擬淘寶登錄的實(shí)現(xiàn)過程示例及解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2021-10-10
  • Python協(xié)程實(shí)踐分享

    Python協(xié)程實(shí)踐分享

    這篇文章主要分享的是Python協(xié)程實(shí)踐,協(xié)程簡(jiǎn)單來(lái)說就是一個(gè)更加輕量級(jí)的線程,并且不由操作系統(tǒng)內(nèi)核管理,完全由程序所控制,下文相關(guān)介紹需要的朋友可以參考一下
    2022-05-05
  • 詳解Python GUI工具取色器

    詳解Python GUI工具取色器

    作為Python開發(fā)者,你遲早都會(huì)用到圖形用戶界面來(lái)開發(fā)應(yīng)用。本文將推薦Python GUI工具取色器的一些知識(shí),感興趣的朋友一起看看吧
    2021-06-06
  • pytorch讀取圖像數(shù)據(jù)轉(zhuǎn)成opencv格式實(shí)例

    pytorch讀取圖像數(shù)據(jù)轉(zhuǎn)成opencv格式實(shí)例

    這篇文章主要介紹了pytorch讀取圖像數(shù)據(jù)轉(zhuǎn)成opencv格式實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-06-06
  • 使用pyinstaller打包py文件的實(shí)現(xiàn)步驟

    使用pyinstaller打包py文件的實(shí)現(xiàn)步驟

    PyInstaller是一個(gè)用于將Python腳本打包成獨(dú)立可執(zhí)行文件的工具,本文主要介紹了使用pyinstaller打包py文件,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-03-03
  • Python importlib模塊重載使用方法詳解

    Python importlib模塊重載使用方法詳解

    這篇文章主要介紹了Python importlib模塊重載使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Python的多維空數(shù)組賦值方法

    Python的多維空數(shù)組賦值方法

    下面小編就為大家分享一篇Python的多維空數(shù)組賦值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2018-04-04
  • Python摸魚神器之利用樹莓派opencv人臉識(shí)別自動(dòng)控制電腦顯示桌面

    Python摸魚神器之利用樹莓派opencv人臉識(shí)別自動(dòng)控制電腦顯示桌面

    這篇文章主要介紹了Python摸魚神器樹莓派opencv人臉識(shí)別自動(dòng)控制電腦顯示桌面,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • 使用python框架Scrapy爬取數(shù)據(jù)的操作步驟

    使用python框架Scrapy爬取數(shù)據(jù)的操作步驟

    Scrapy是一個(gè)基于Python的強(qiáng)大的開源網(wǎng)絡(luò)爬蟲框架,用于從網(wǎng)站上抓取信息,它提供了廣泛的功能,使得爬取和分析數(shù)據(jù)變得相對(duì)容易,本文小編將給給大家介紹一下如何使用python框架Scrapy爬取數(shù)據(jù),需要的朋友可以參考下
    2023-10-10

最新評(píng)論

龙陵县| 襄垣县| 平昌县| 璧山县| 崇明县| 富平县| 嘉荫县| 玉林市| 阳原县| 长丰县| 丹巴县| 五家渠市| 都昌县| 乐都县| 海伦市| 慈溪市| 兴国县| 班玛县| 景泰县| 宣化县| 松江区| 克拉玛依市| 古蔺县| 甘孜| 鄯善县| 三明市| 闽侯县| 科技| 金阳县| 茌平县| 永新县| 通辽市| 深泽县| 阿合奇县| 志丹县| 安吉县| 庆阳市| 上蔡县| 本溪| 会昌县| 荥经县|