安裝
pip install pyquery
或者直接在PyCharm中import pyquery 提示沒有的時候點擊install就ok啦
初始化
在這裏介紹四種初始化方式。
(1)直接字符串
from pyquery import PyQuery as pq
doc = pq("<html></html>")
pq 參數可以直接傳入 HTML 代碼,doc 現在就相當於 jQuery 裏面的 $ 符號了。
(2)lxml.etree
from lxml import etree
doc = pq(etree.fromstring("<html></html>"))
可以首先用 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 的文件,文件內容爲
<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()
運行結果
<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 語法)的對象!
屬性操作
你可以完全按照 jQuery 的語法來進行 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")
運行結果
hello
<p id="plop" class="hello"/>
<p id="hello" class="hello"/>
再看一個例子
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'})
運行結果
<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 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
運行結果
<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 方法返回對象列表,或者用 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
<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>
不過最常用的還是 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,樣樣通。