欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python爬蟲之BeautifulSoup 使用select方法詳解

 更新時(shí)間:2017年10月23日 14:38:40   作者:古怪的一陣風(fēng)  
本篇文章主要介紹了python爬蟲之BeautifulSoup 使用select方法詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文介紹了python爬蟲之BeautifulSoup 使用select方法詳解 ,分享給大家。具體如下:

<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  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1"><!-- Elsie --></a>,
<a  rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a  rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

我們在寫 CSS 時(shí),標(biāo)簽名不加任何修飾,類名前加點(diǎn),id名前加 #,在這里我們也可以利用類似的方法來篩選元素,用到的方法是 soup.select(),返回類型是 list

(1)通過標(biāo)簽名查找

print soup.select('title') 
#[<title>The Dormouse's story</title>]
 
print soup.select('a')
#[<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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>]
 
print soup.select('b')
#[<b>The Dormouse's story</b>]

(2)通過類名查找

print soup.select('.sister')
#[<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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>]

(3)通過 id 名查找

print soup.select('#link1')
#[<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]

(4)組合查找

組合查找即和寫 class 文件時(shí),標(biāo)簽名與類名、id名進(jìn)行的組合原理是一樣的,例如查找 p 標(biāo)簽中,id 等于 link1的內(nèi)容,二者需要用空格分開

print soup.select('p #link1')
#[<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]

直接子標(biāo)簽查找

print soup.select("head > title")
#[<title>The Dormouse's story</title>]

(5)屬性查找

查找時(shí)還可以加入屬性元素,屬性需要用中括號(hào)括起來,注意屬性和標(biāo)簽屬于同一節(jié)點(diǎn),所以中間不能加空格,否則會(huì)無法匹配到。

print soup.select("head > title")
#[<title>The Dormouse's story</title>]
 
print soup.select('a[ rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ]')
#[<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]

同樣,屬性仍然可以與上述查找方式組合,不在同一節(jié)點(diǎn)的空格隔開,同一節(jié)點(diǎn)的不加空格

print soup.select('p a[ rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ]')
#[<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python讀取中文路徑出現(xiàn)亂碼的問題解決

    Python讀取中文路徑出現(xiàn)亂碼的問題解決

    本文主要介紹了Python讀取中文路徑出現(xiàn)亂碼的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • 通過python的matplotlib包將Tensorflow數(shù)據(jù)進(jìn)行可視化的方法

    通過python的matplotlib包將Tensorflow數(shù)據(jù)進(jìn)行可視化的方法

    今天小編就為大家分享一篇通過python的matplotlib包將Tensorflow數(shù)據(jù)進(jìn)行可視化的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python?遞歸式實(shí)現(xiàn)二叉樹前序,中序,后序遍歷

    Python?遞歸式實(shí)現(xiàn)二叉樹前序,中序,后序遍歷

    這篇文章主要介紹了Python?遞歸式實(shí)現(xiàn)二叉樹前序,中序,后序遍歷,更多相關(guān)資料,需要的小伙伴可以參考下面具體的文章內(nèi)容
    2022-03-03
  • Python實(shí)現(xiàn)的rsa加密算法詳解

    Python實(shí)現(xiàn)的rsa加密算法詳解

    這篇文章主要介紹了Python實(shí)現(xiàn)的rsa加密算法,結(jié)合完整實(shí)例形式分析了Python實(shí)現(xiàn)rsa加密算法的原理、步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • 使用scrapy實(shí)現(xiàn)增量式爬取方式

    使用scrapy實(shí)現(xiàn)增量式爬取方式

    這篇文章主要介紹了使用scrapy實(shí)現(xiàn)增量式爬取方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Python提取頻域特征知識(shí)點(diǎn)淺析

    Python提取頻域特征知識(shí)點(diǎn)淺析

    在本文里我們給大家分享了關(guān)于Python提取頻域特征的相關(guān)知識(shí)點(diǎn)以及基礎(chǔ)內(nèi)容,需要的朋友們跟著學(xué)習(xí)下。
    2019-03-03
  • 教你如何使用Python Tkinter庫制作記事本

    教你如何使用Python Tkinter庫制作記事本

    讓我們看看如何使用 Tkinter 在 Python 中創(chuàng)建一個(gè)簡單的記事本.這個(gè)記事本 GUI 將包含各種菜單,如文件和編輯,使用這些菜單可以完成保存文件、打開文件、編輯、剪切和粘貼等所有功能,需要的朋友可以參考下
    2021-06-06
  • python創(chuàng)建字典(dict)的幾種方法小結(jié)(含代碼示例)

    python創(chuàng)建字典(dict)的幾種方法小結(jié)(含代碼示例)

    字典(Dictionary)是Python中一種非常靈活的數(shù)據(jù)結(jié)構(gòu),用于存儲(chǔ)鍵值對(key-value pairs),在Python中創(chuàng)建字典有多種方法,每種方法都有其特定的使用場景和優(yōu)勢,本文將詳細(xì)介紹Python中創(chuàng)建字典的幾種常見方法,需要的朋友可以參考下
    2024-09-09
  • pyecharts動(dòng)態(tài)軌跡圖的實(shí)現(xiàn)示例

    pyecharts動(dòng)態(tài)軌跡圖的實(shí)現(xiàn)示例

    這篇文章主要介紹了pyecharts動(dòng)態(tài)軌跡圖的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • python持久化存儲(chǔ)文件操作方法

    python持久化存儲(chǔ)文件操作方法

    本文給大家分享python持久化存儲(chǔ)文件操作方法,給大家講解存儲(chǔ)文件的重要性,指針的基本概念及關(guān)閉文件的方法,介紹文件的創(chuàng)建和刪除技巧,感興趣的朋友一起看看吧
    2021-06-06

最新評(píng)論