溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python中PyQuery如何使用

發(fā)布時(shí)間:2021-08-11 16:07:13 來(lái)源:億速云 閱讀:126 作者:Leah 欄目:編程語(yǔ)言

本篇文章給大家分享的是有關(guān)Python中PyQuery如何使用,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

安裝

pip install pyquery

初始化

在這里介紹四種初始化方式。

(1)直接字符串

from pyquery import PyQuery as pq  doc = pq("<html></html>")

pq 參數(shù)可以直接傳入 HTML 代碼,doc 現(xiàn)在就相當(dāng)于 jQuery 里面的 $ 符號(hào)了。

(2)lxml.etree

from lxml import etree  doc = pq(etree.fromstring("<html></html>"))

可以首先用 lxml 的 etree 處理一下代碼,這樣如果你的 HTML 代碼出現(xiàn)一些不完整或者疏漏,都會(huì)自動(dòng)轉(zhuǎn)化為完整清晰結(jié)構(gòu)的  HTML代碼。

(3)直接傳URL

from pyquery import PyQuery as pq  doc = pq('http://www.baidu.com')

這里就像直接請(qǐng)求了一個(gè)網(wǎng)頁(yè)一樣,類似用 urllib2 來(lái)直接請(qǐng)求這個(gè)鏈接,得到 HTML 代碼。

(4)傳文件

from pyquery import PyQuery as pq  doc = pq(filename='hello.html')

可以直接傳某個(gè)路徑的文件名。

快速體驗(yàn)

現(xiàn)在我們以本地文件為例,傳入一個(gè)名字為 hello.html 的文件,文件內(nèi)容為

<div>      <ul>           <li class="item-0">first item</li>           <li class="item-1"><a href="link2.html">second item</a></li>           <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>           <li class="item-1 active"><a href="link4.html">fourth item</a></li>           <li class="item-0"><a href="link5.html">fifth item</a></li>       </ul>  </div>

編寫如下程序

from pyquery import PyQuery as pq  doc = pq(filename='hello.html')  print doc.html()  print type(doc)  li = doc('li')  print type(li)  print li.text()

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

<ul>           <li class="item-0">first item</li>           <li class="item-1"><a href="link2.html">second item</a></li>           <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>           <li class="item-1 active"><a href="link4.html">fourth item</a></li>           <li class="item-0"><a href="link5.html">fifth item</a></li>       </ul>  <class 'pyquery.pyquery.PyQuery'>  <class 'pyquery.pyquery.PyQuery'>  first item second item third item fourth item fifth item

看,回憶一下 jQuery 的語(yǔ)法,是不是運(yùn)行結(jié)果都是一樣的呢?

在這里我們注意到了一點(diǎn),PyQuery 初始化之后,返回類型是 PyQuery,利用了選擇器篩選一次之后,返回結(jié)果的類型依然還是 PyQuery,這簡(jiǎn)直和  jQuery 如出一轍,不能更贊!然而想一下 BeautifulSoup 和 XPath 返回的是什么?列表!一種不能再進(jìn)行二次篩選(在這里指依然利用  BeautifulSoup 或者 XPath 語(yǔ)法)的對(duì)象!

然而比比 PyQuery,哦我簡(jiǎn)直太愛(ài)它了!

屬性操作

你可以完全按照 jQuery 的語(yǔ)法來(lái)進(jìn)行 PyQuery 的操作。

from pyquery import PyQuery as pq     p = pq('<p id="hello" class="hello"></p>')('p')  print p.attr("id")  print p.attr("id", "plop")  print p.attr("id", "hello")

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

hello  <p id="plop" class="hello"/>  <p id="hello" class="hello"/>

再來(lái)一發(fā)

from pyquery import PyQuery as pq     p = pq('<p id="hello" class="hello"></p>')('p')  print p.addClass('beauty')  print p.removeClass('hello')  print p.css('font-size', '16px')  print p.css({'background-color': 'yellow'})

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

<p id="hello" class="hello beauty"/>  <p id="hello" class="beauty"/>  <p id="hello" class="beauty" style="font-size: 16px"/>  <p id="hello" class="beauty" style="font-size: 16px; background-color: yellow"/>

依舊是那么優(yōu)雅與自信!

在這里我們發(fā)現(xiàn)了,這是一連串的操作,而 p 是一直在原來(lái)的結(jié)果上變化的。

因此執(zhí)行上述操作之后,p 本身也發(fā)生了變化。

DOM操作

同樣的原汁原味的 jQuery 語(yǔ)法

from pyquery import PyQuery as pq     p = pq('<p id="hello" class="hello"></p>')('p')  print p.append(' check out <a href="http://reddit.com/r/python"><span>reddit</span></a>')  print p.prepend('Oh yes!')  d = pq('<div class="wrap"><div id="test"><a href="http://cuiqingcai.com">Germy</a></div></div>')  p.prependTo(d('#test'))  print p  print d  d.empty()  print d

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

<p id="hello" class="hello"> check out <a href="http://reddit.com/r/python"><span>reddit</span></a></p>  <p id="hello" class="hello">Oh yes! check out <a href="http://reddit.com/r/python"><span>reddit</span></a></p>  <p id="hello" class="hello">Oh yes! check out <a href="http://reddit.com/r/python"><span>reddit</span></a></p>  <div class="wrap"><div id="test"><p id="hello" class="hello">Oh yes! check out <a href="http://reddit.com/r/python"><span>reddit</span></a></p><a href="http://cuiqingcai.com">Germy</a></div></div>  <div class="wrap"/>

這不需要多解釋了吧。

DOM 操作也是與 jQuery 如出一轍。

遍歷

遍歷用到 items 方法返回對(duì)象列表,或者用 lambda

from pyquery import PyQuery as pq  doc = pq(filename='hello.html')  lis = doc('li')  for li in lis.items():      print li.html()     print lis.each(lambda e: e)

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

first item  <a href="link2.html">second item</a>  <a href="link3.html"><span class="bold">third item</span></a>  <a href="link4.html">fourth item</a>  <a href="link5.html">fifth item</a>  <li class="item-0">first item</li>  <li class="item-1"><a href="link2.html">second item</a></li>  <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>  <li class="item-1 active"><a href="link4.html">fourth item</a></li>  <li class="item-0"><a href="link5.html">fifth item</a></li>

不過(guò)最常用的還是 items 方法

網(wǎng)頁(yè)請(qǐng)求

PyQuery 本身還有網(wǎng)頁(yè)請(qǐng)求功能,而且會(huì)把請(qǐng)求下來(lái)的網(wǎng)頁(yè)代碼轉(zhuǎn)為 PyQuery 對(duì)象。

from pyquery import PyQuery as pq  print pq('http://cuiqingcai.com/', headers={'user-agent': 'pyquery'})  print pq('http://httpbin.org/post', {'foo': 'bar'}, method='post', verify=True)

以上就是Python中PyQuery如何使用,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI