當前位置:
首頁 > 知識 > Python 爬蟲神器 PyQuery 的使用方法

Python 爬蟲神器 PyQuery 的使用方法

(點擊

上方藍字

,快速關注我們)




來源:xiaomayi2012


segmentfault.com/a/1190000005182997


如有好文章投稿,請點擊 → 這裡了解詳情




前言



你是否覺得 XPath 的用法多少有點晦澀難記呢?




你是否覺得 BeautifulSoup 的語法多少有些慳吝難懂呢?




你是否甚至還在苦苦研究正則表達式卻因為少些了一個點而抓狂呢?




你是否已經有了一些前端基礎了解選擇器卻與另外一些奇怪的選擇器語法混淆了呢?




嗯,那麼,前端大大們的福音來了,PyQuery 來了,乍聽名字,你一定聯想到了 jQuery,如果你對 jQuery 熟悉,那麼 PyQuery 來解析文檔就是不二之選!包括我在內!



PyQuery 是 Python 仿照 jQuery 的嚴格實現。語法與 jQuery 幾乎完全相同,所以不用再去費心去記一些奇怪的方法了。




天下竟然有這等好事?我都等不及了!




安裝




有這等神器還不趕緊安裝了!來!




pip install pyquery




參考來源




本文內容參考官方文檔,更多內容,大家可以去官方文檔學習,畢竟那裡才是最原汁原味的。




目前版本 1.2.4 (2016/3/24)



官方文檔 (https://pythonhosted.org/pyquery/)




簡介





pyquery allows you to make jquery queries on xml documents. The API is


as much as possible the similar to jquery. pyquery uses lxml for fast

xml and html manipulation. This is not (or at least not yet) a library


to produce or interact with javascript code. I just liked the jquery


API and I missed it in python so I told myself 「Hey let』s make jquery


in python」. This is the result. It can be used for many purposes, one


idea that I might try in the future is to use it for templating with

pure http templates that you modify using pyquery. I can also be used


for web scrapping or for theming applications with Deliverance.




pyquery 可讓你用 jQuery 的語法來對 xml 進行操作。這I和 jQuery 十分類似。如果利用 lxml,pyquery 對 xml 和 html 的處理將更快。




這個庫不是(至少還不是)一個可以和 JavaScript交互的代碼庫,它只是非常像 jQuery API 而已。



初始化




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




(1)直接字元串





from

pyquery

import

PyQuery

as

pq


doc

=

pq

(

""

)




pq 參數可以直接傳入 HTML 代碼,doc 現在就相當於 jQuery 裡面的 $ 符號了。




(2)lxml.etree





from

lxml

import

etree


doc

=

pq

(

etree

.

fromstring

(

""

))




可以首先用 lxml 的 etree 處理一下代碼,這樣如果你的 HTML 代碼出現一些不完整或者疏漏,都會自動轉化為完整清晰結構的 HTML代碼。




(3)直接傳URL





from pyquery import PyQuery

as

pq


doc

=

pq

(

"http://www.baidu.com"

)




這裡就像直接請求了一個網頁一樣,類似用 urllib2 來直接請求這個鏈接,得到 HTML 代碼。




(4)傳文件





from pyquery import PyQuery

as

pq


doc

=

pq

(

filename

=

"hello.html"

)




可以直接傳某個路徑的文件名。




快速體驗




現在我們以本地文件為例,傳入一個名字為 hello.html 的文件,文件內容為










編寫如下程序





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

()




運行結果





<

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 的語法,是不是運行結果都是一樣的呢?




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




然而比比 PyQuery,哦我簡直太愛它了!




屬性操作




你可以完全按照 jQuery 的語法來進行 PyQuery 的操作。





from

pyquery

import

PyQuery

as

pq



p

=

pq

(

"

"

)(

"p"

)


print

p

.

attr

(

"id"

)


print

p

.

attr

(

"id"

,

"plop"

)


print

p

.

attr

(

"id"

,

"hello"

)




運行結果





hello


<

p

id

=

"plop"

class

=

"hello"

/>


<

p

id

=

"hello"

class

=

"hello"

/>




再來一發





from

pyquery

import

PyQuery

as

pq



p

=

pq

(

"

"

)(

"p"

)


print

p

.

addClass

(

"beauty"

)


print

p

.

removeClass

(

"hello"

)


print

p

.

css

(

"font-size"

,

"16px"

)


print

p

.

css

({

"background-color"

:

"yellow"

})




運行結果





<

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"

/>




依舊是那麼優雅與自信!




在這裡我們發現了,這是一連串的操作,而 p 是一直在原來的結果上變化的。




因此執行上述操作之後,p 本身也發生了變化。




DOM操作




同樣的原汁原味的 jQuery 語法





from

pyquery

import

PyQuery

as

pq



p

=

pq

(

"

"

)(

"p"

)


print

p

.

append

(

" check out reddit"

)


print

p

.

prepend

(

"Oh yes!"

)


d

=

pq

(

"

"

)


p

.

prependTo

(

d

(

"#test"

))


print

p


print

d


d

.

empty

()


print

d




運行結果





id

=

"hello"

class

=

"hello"

>

check out

href

=

"http://reddit.com/r/python"

>

reddit


id

=

"hello"

class

=

"hello"

>

Oh yes! check out

href

=

"http://reddit.com/r/python"

>

reddit


id

=

"hello"

class

=

"hello"

>

Oh yes! check out

href

=

"http://reddit.com/r/python"

>

reddit


class

=

"wrap"

>

id

=

"test"

>

id

=

"hello"

class

=

"hello"

>

Oh yes! check out

href

=

"http://reddit.com/r/python"

>

reddit

href

=

"http://cuiqingcai.com"

>

Germy


class

=

"wrap"

/>




這不需要多解釋了吧。




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




遍歷




遍歷用到 items 方法返回對象列表,或者用 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

)




運行結果





first

item


href

=

"link2.html"

>

second item


href

=

"link3.html"

>

class

=

"bold"

>

third item


href

=

"link4.html"

>

fourth item


href

=

"link5.html"

>

fifth item


  • class

    =

    "item-0"

    >

    first item


  • class

    =

    "item-1"

    >

    href

    =

    "link2.html"

    >

    second item


  • class

    =

    "item-0 active"

    >

    href

    =

    "link3.html"

    >

    class

    =

    "bold"

    >

    third item


  • class

    =

    "item-1 active"

    >

    href

    =

    "link4.html"

    >

    fourth item


  • class

    =

    "item-0"

    >

    href

    =

    "link5.html"

    >

    fifth item




  • 不過最常用的還是 items 方法




    網頁請求




    PyQuery 本身還有網頁請求功能,而且會把請求下來的網頁代碼轉為 PyQuery 對象。





    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

    )




    感受一下,GET,POST,樣樣通。




    Ajax




    PyQuery 同樣支持 Ajax 操作,帶有 get 和 post 方法,不過不常用,一般我們不會用 PyQuery 來做網路請求,僅僅是用來解析。




    PyQueryAjax




    API




    最後少不了的,API大放送。




    API (https://pythonhosted.org/pyquery/api.html)




    原汁原味最全的API,都在裡面了!如果你對 jQuery 語法不熟,強烈建議先學習下 jQuery,再回來看 PyQuery,你會感到異常親切!




    結語




    用完了 PyQuery,我已經深深愛上了他!




    你呢?




    看完本文有收穫?請轉

    發分享給更多人


    關注「Python開發者」,提升Python技能


    喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

    本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


    請您繼續閱讀更多來自 Python開發者 的精彩文章:

    Python 圖像處理庫 Pillow 入門
    Python + Django 如何支撐了 7 億月活用戶的 Instagram?
    使用 VASPy 快速處理 VASP 文件以及數據可視化
    Django 通道簡要介紹

    TAG:Python開發者 |

    您可能感興趣

    Pytorch神器
    Python可視化神器——pyecharts的超詳細使用指南!
    Mac效率神器Alfred系列教程-iTunes Mini Player
    Photoshop調色神器!一鍵模擬Vsco和Instagram濾鏡效果
    SSH客戶端神器MobaXterm,該拋棄putty、Xshell和CRT了
    CPU優化神器Steam CPUcores
    最適合冬日抱團取暖的「神器」來了Here Comes a Great Heater in Winter
    小場神器 耐克MercurialX Superfly VI小場足球鞋曝光
    營銷神器!Salesforce宣布更新Sales Cloud多項功能
    或許是最低調的神器品牌 Daskeyboard 4 Ultimate茶軸評測
    血管外血流調節神器:Exo-Vascular Treatment?
    EsportsChain創始人Pechi:區塊鏈技術可能是防外掛神器
    NeuroHalo 智能美髮神器
    Android逆向之旅-Hook神器家族的Frida工具使用詳解
    真正的防水泡麵神器Kindle Paperwhite評論
    被低估的神器 適馬SD Quattro
    iPhone被完全破解神器GrayKey現身:蘋果修復漏洞
    iPhoneX必備神器 AirPower無線底座來了
    Jabra Elite Sport臻躍 真無線神器!
    Fendi小牛皮鞋,Rimowa登機箱,Foreo洗臉神器