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

python網(wǎng)絡(luò)爬蟲精解之pyquery的使用說明

 更新時間:2021年09月26日 16:25:52   作者:小狐貍夢想去童話鎮(zhèn)  
PyQuery是一個類似于jQuery的解析網(wǎng)頁工具,使用lxml操作xml和html文檔,它的語法和jQuery很像。和XPATH,Beautiful Soup比起來,PyQuery更加靈活,提供增加節(jié)點的class信息,移除某個節(jié)點,提取文本信息等功能

pyquery的使用

一、pyquery的介紹

使用pyquery需要在Web和了解jQuery的基礎(chǔ)上,使用該CSS選擇器。

二、pyquery的使用

1、初始化工作

使用pyquery初始化的方式有很多,傳入的參數(shù)可以是字符串,也可以是URL和文件名,下面將一一介紹初始化方法。

字符串

html = '''
<html>

<head>
  <meta charset="utf-8">
  <title>test02.html</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
  <div id="container">
    <iframe id="iframe" sandbox="allow-scripts" src="/files/%E7%88%AC%E8%99%AB%E5%86%99%E4%BD%9C%E4%BB%A3%E7%A0%81%E6%B5%8B%E8%AF%95/test02.html"></iframe>
  </div>
</body>

</html>
'''
from pyquery import PyQuery as pq
doc = pq(html)
print(doc('title'))

【運行結(jié)果】

<title>test02.html</title>

URL

URL以CSDN首頁地址為例:

from pyquery import PyQuery as pq
doc = pq(url = 'https://www.csdn.net/')
print(doc('title'))

【運行結(jié)果】

<title>CSDN - 專業(yè)開發(fā)者社區(qū)</title>

文件初始化

我們將以下字符串保存為一個HTML文件,通過文件的形式進行初始化。

【test02.html】

<bookstore>
  <book>
    <title lang="eng">Harry Potter</title>
    <price>29.99</price>
  </book>

  <book>
    <title lang="eng">Learning XML</title>
    <price>39.95</price>
  </book>
</bookstore>
from pyquery import PyQuery as pq
doc = pq(filename = 'test02.html')
print(doc('title'))

【運行結(jié)果】

<title lang="eng">Harry Potter</title>
<title lang="eng">Learning XML</title>

2、查找節(jié)點

(1)查找子節(jié)點

查找子節(jié)點時需要用到find()方法,此時傳入的參數(shù)是CSS選擇器。

from pyquery import PyQuery as pq
doc = pq(filename = 'test02.html')
item = doc('book')
print(item)
lis1 = item.find('title')
lis2 = item.find('price')
print(lis1)
print(lis2)

【運行結(jié)果】

<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>

<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>

<title lang="eng">Harry Potter</title>
<title lang="eng">Learning XML</title>

<price>29.99</price>
<price>39.95</price>
可以看到,我們首先匹配的是book節(jié)點,然后匹配book節(jié)點下的子節(jié)點title和price。

其實使用find方法匹配的是所有的子孫節(jié)點,如果只是單純匹配子節(jié)點可以使用children方法。

(2)匹配父節(jié)點

使用parent()方法,如果是要匹配祖先節(jié)點,則需要使用parents()方法。

(3)匹配兄弟節(jié)點

可以使用siblings()方法。

3、遍歷

對于獲取到的內(nèi)容如果是單個節(jié)點,則可以直接轉(zhuǎn)換為字符串類型,而對于獲取到多個節(jié)點,因其類型為PyQuery類型,需要對獲取到的數(shù)據(jù)進行遍歷,這是需要調(diào)用items()方法。

from pyquery import PyQuery as pq
doc = pq(filename = 'test02.html')
items = doc('title').items()
print(items)
print(type(items))
for i in items:
    print(type(i))
    print(i)
    

【運行結(jié)果】

<generator object PyQuery.items at 0x000002B79E13EF48>
<class 'generator'>
<class 'pyquery.pyquery.PyQuery'>
<title lang="eng">Harry Potter</title>

<class 'pyquery.pyquery.PyQuery'>
<title lang="eng">Learning XML</title>

4、獲取信息

(1)獲取屬性

使用attr()方法

from pyquery import PyQuery as pq
doc = pq(filename = 'test02.html')
items = doc('title')
for i in items.items():
    print(i.attr('lang'))   

【運行結(jié)果】

eng
eng

遍歷獲取到的數(shù)據(jù),就能獲得所有title節(jié)點的land屬性值。

(2)獲取文本

使用text()方法

from pyquery import PyQuery as pq
doc = pq(filename = 'test02.html')
items = doc('title')
for i in items.items():
    print(i.text())  

【運行結(jié)果】

Harry Potter
Learning XML

同樣是遍歷,獲取到每一個title節(jié)點的文本值。

5、節(jié)點操作

(1)為某個節(jié)點添加或刪除一個class

調(diào)用的方法為addClass和removeClass

from pyquery import PyQuery as pq
doc = pq(filename = 'test02.html')
items = doc('title')
for i in items.items():
    print(i)
    i.addClass('book01')
    print(i)
    i.removeClass('book01')
    print(i)

【運行結(jié)果】

<title lang="eng">Harry Potter</title>

<title lang="eng" class="book01">Harry Potter</title>

<title lang="eng" class="">Harry Potter</title>

<title lang="eng">Learning XML</title>

<title lang="eng" class="book01">Learning XML</title>

<title lang="eng" class="">Learning XML</title>

可以看到,首先是打印最初始的title節(jié)點,加上class屬性后再次打印,去掉class屬性后再次打印。

(2)attr、text、html

attr:用來改變屬性值;

text:用來改變文本值;

html:用來改變節(jié)點值;

(3)remove

移除不需要的節(jié)點值,將整個節(jié)點移除。

6、偽類選擇器

支持多種偽類選擇器,例如選擇第一個節(jié)點、最后一個節(jié)點、奇數(shù)節(jié)點、偶數(shù)節(jié)點、以及包含指定文本的節(jié)點等。

到此這篇關(guān)于python網(wǎng)絡(luò)爬蟲精解之pyquery的使用說明的文章就介紹到這了,更多相關(guān)python pyquery 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • numpy 計算兩個數(shù)組重復程度的方法

    numpy 計算兩個數(shù)組重復程度的方法

    今天小編就為大家分享一篇numpy 計算兩個數(shù)組重復程度的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • Python最長公共子串算法實例

    Python最長公共子串算法實例

    這篇文章主要介紹了Python最長公共子串算法,實例分析了Python字符串操作的技巧,需要的朋友可以參考下
    2015-03-03
  • Python?property裝飾器使用案例介紹

    Python?property裝飾器使用案例介紹

    這篇文章主要介紹了Python?@property裝飾器的用法,在Python中,可以通過@property裝飾器將一個方法轉(zhuǎn)換為屬性,從而實現(xiàn)用于計算的屬性,下面文章圍繞主題展開更多相關(guān)詳情,感興趣的小伙伴可以參考一下
    2022-10-10
  • ???????Python?入門學習之函數(shù)式編程

    ???????Python?入門學習之函數(shù)式編程

    這篇文章主要介紹了???????Python?入門學習之函數(shù)式編程,?Python?中的函數(shù)式編程技術(shù)進行了簡單的入門介紹,下文詳細內(nèi)容需要的小伙伴可以參考一下
    2022-05-05
  • python使用pyshp讀寫shp文件的實現(xiàn)

    python使用pyshp讀寫shp文件的實現(xiàn)

    本文主要介紹了python使用pyshp讀寫shp文件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • Python實現(xiàn)圖片識別加翻譯功能

    Python實現(xiàn)圖片識別加翻譯功能

    這篇文章主要介紹了Python使用百度AI接口實現(xiàn)圖片識別加翻譯功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • python中requests模擬登錄的三種方式(攜帶cookie/session進行請求網(wǎng)站)

    python中requests模擬登錄的三種方式(攜帶cookie/session進行請求網(wǎng)站)

    這篇文章主要介紹了python中requests模擬登錄的三種方式(攜帶cookie/session進行請求網(wǎng)站),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • 樹莓派用python中的OpenCV輸出USB攝像頭畫面

    樹莓派用python中的OpenCV輸出USB攝像頭畫面

    這篇文章主要為大家詳細介紹了樹莓派用python中的OpenCV輸出USB攝像頭畫面,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • python 數(shù)據(jù)清洗之數(shù)據(jù)合并、轉(zhuǎn)換、過濾、排序

    python 數(shù)據(jù)清洗之數(shù)據(jù)合并、轉(zhuǎn)換、過濾、排序

    這篇文章主要介紹了python 數(shù)據(jù)清洗之數(shù)據(jù)合并、轉(zhuǎn)換、過濾、排序的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Python中正反斜杠(‘/’和‘\’)的意義與用法

    Python中正反斜杠(‘/’和‘\’)的意義與用法

    這篇文章主要給大家介紹了關(guān)于Python中正反斜杠(‘/’和‘\’)的意義與使用方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-08-08

最新評論