亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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>
"""

我們?cè)趯?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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python連接clickhouse數(shù)據(jù)庫的兩種方式小結(jié)

    python連接clickhouse數(shù)據(jù)庫的兩種方式小結(jié)

    這篇文章主要介紹了python連接clickhouse數(shù)據(jù)庫的兩種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • python格式化輸出實(shí)例(居中、靠右及靠左對(duì)齊)

    python格式化輸出實(shí)例(居中、靠右及靠左對(duì)齊)

    所謂格式化輸出就是數(shù)據(jù)按照某種特殊的格式和要求進(jìn)行輸出,下面這篇文章主要給大家介紹了關(guān)于python格式化輸出(居中、靠右及靠左對(duì)齊)的相關(guān)資料,文中介紹了format方式、其他擴(kuò)展寫法以及'%'方式,需要的朋友可以參考下
    2022-04-04
  • python35種繪圖函數(shù)詳細(xì)總結(jié)

    python35種繪圖函數(shù)詳細(xì)總結(jié)

    Python有許多用于繪圖的函數(shù)和庫,比如Matplotlib,Plotly,Bokeh,Seaborn等,這只是一些常用的繪圖函數(shù)和庫,Python還有其他繪圖工具,如Pandas、ggplot等,選擇適合你需求的庫,可以根據(jù)你的數(shù)據(jù)類型、圖形需求和個(gè)人偏好來決定,本文給大家總結(jié)了python35種繪圖函數(shù)
    2023-08-08
  • 基于Python編寫端口進(jìn)程管理工具

    基于Python編寫端口進(jìn)程管理工具

    這篇文章主要為大家介紹了如何使用Python編寫一個(gè)用于端口管理和進(jìn)程管理的GUI工具,它可以顯示當(dāng)前系統(tǒng)上所有開放的端口信息,感興趣的可以了解下
    2025-01-01
  • 怎樣使用Python腳本日志功能

    怎樣使用Python腳本日志功能

    Python通過logging模塊提供日志功能,關(guān)于logging模塊的使用網(wǎng)絡(luò)上已經(jīng)有很多詳細(xì)的資料,今天這里要分享的是怎樣在實(shí)際工程中使用日志功能。下面一起來學(xué)習(xí)學(xué)習(xí)。
    2016-08-08
  • 使用Selenium破解新浪微博的四宮格驗(yàn)證碼

    使用Selenium破解新浪微博的四宮格驗(yàn)證碼

    今天小編就為大家分享一篇關(guān)于使用Selenium破解新浪微博的四宮格驗(yàn)證碼的文章,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • python類型強(qiáng)制轉(zhuǎn)換long to int的代碼

    python類型強(qiáng)制轉(zhuǎn)換long to int的代碼

    python的int型最大值和系統(tǒng)有關(guān),32位和64位系統(tǒng)結(jié)果是不同的,分別為2的31次方減1和2的63次方減1,可以通過sys.maxint查看此值
    2013-02-02
  • 你應(yīng)該知道的Python3.6、3.7、3.8新特性小結(jié)

    你應(yīng)該知道的Python3.6、3.7、3.8新特性小結(jié)

    這篇文章主要介紹了你應(yīng)該知道的Python3.6、3.7、3.8新特性小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Python中的pyecharts庫使用總結(jié)

    Python中的pyecharts庫使用總結(jié)

    這篇文章主要介紹了Python中的pyecharts庫使用總結(jié),Pyecharts 提供了一個(gè)簡單而直觀的 API 接口,使得使用者無需了解復(fù)雜的 JavaScript 語法,即可通過 Python 代碼實(shí)現(xiàn)高度定制化的圖表設(shè)計(jì),需要的朋友可以參考下
    2023-12-12
  • python實(shí)操案例練習(xí)(八)

    python實(shí)操案例練習(xí)(八)

    這篇文章主要介紹了python實(shí)操案例練習(xí),本篇文章主要分享的案例內(nèi)容有記錄用戶登錄日志、模擬淘寶客服自動(dòng)回復(fù),下面詳細(xì)的內(nèi)容,需要的小伙伴可以參考一下。希望對(duì)你有所幫助
    2022-02-02

最新評(píng)論