python解析html開發(fā)庫pyquery使用方法
例如
<div id="info">
<span><span class='pl'>導(dǎo)演</span>: <a href="/celebrity/1047989/" rel="v:directedBy">湯姆·提克威</a> / <a href="/celebrity/1161012/" rel="v:directedBy">拉娜·沃卓斯基</a> / <a href="/celebrity/1013899/" rel="v:directedBy">安迪·沃卓斯基</a></span><br/>
<span><span class='pl'>編劇</span>: <a href="/celebrity/1047989/">湯姆·提克威</a> / <a href="/celebrity/1013899/">安迪·沃卓斯基</a> / <a href="/celebrity/1161012/">拉娜·沃卓斯基</a></span><br/>
<span><span class='pl'>主演</span>: <a href="/celebrity/1054450/" rel="v:starring">湯姆·漢克斯</a> / <a href="/celebrity/1054415/" rel="v:starring">哈莉·貝瑞</a> / <a href="/celebrity/1019049/" rel="v:starring">吉姆·布勞德本特</a> / <a href="/celebrity/1040994/" rel="v:starring">雨果·維文</a> / <a href="/celebrity/1053559/" rel="v:starring">吉姆·斯特吉斯</a> / <a href="/celebrity/1057004/" rel="v:starring">裴斗娜</a> / <a href="/celebrity/1025149/" rel="v:starring">本·衛(wèi)肖</a> / <a href="/celebrity/1049713/" rel="v:starring">詹姆斯·達(dá)西</a> / <a href="/celebrity/1027798/" rel="v:starring">周迅</a> / <a href="/celebrity/1019012/" rel="v:starring">凱斯·大衛(wèi)</a> / <a href="/celebrity/1201851/" rel="v:starring">大衛(wèi)·吉雅西</a> / <a href="/celebrity/1054392/" rel="v:starring">蘇珊·薩蘭登</a> / <a href="/celebrity/1003493/" rel="v:starring">休·格蘭特</a></span><br/>
<span class="pl">類型:</span> <span property="v:genre">劇情</span> / <span property="v:genre">科幻</span> / <span property="v:genre">懸疑</span><br/>
<span class="pl">官方網(wǎng)站:</span> <a href="http://cloudatlas.warnerbros.com" rel="nofollow" target="_blank">cloudatlas.warnerbros.com</a><br/>
<span class="pl">語言:</span> 英語<br/>
<span class="pl">IMDb鏈接:</span> <a href="http://www.imdb.com/title/tt1371111" target="_blank" rel="nofollow">tt1371111</a><br>
<span class="pl">官方小站:</span>
<a href="http://site.douban.com/202494/" target="_blank">電影《云圖》</a>
</div>
from pyquery import PyQuery as pq
doc=pq(url='http://movie.douban.com/subject/3530403/')
data=doc('.pl')
for i in data:
print pq(i).text()
輸出
導(dǎo)演
編劇
主演
類型:
官方網(wǎng)站:
制片國家/地區(qū):
語言:
上映日期:
片長:
IMDb鏈接:
官方小站:
用法
用戶可以使用PyQuery類從字符串、lxml對(duì)象、文件或者url來加載xml文檔:
>>> from pyquery import PyQuery as pq
>>> from lxml import etree
>>> doc=pq("<html></html>")
>>> doc=pq(etree.fromstring("<html></html>"))
>>> doc=pq(filename=path_to_html_file)
>>> doc=pq(url='http://movie.douban.com/subject/3530403/')
可以像jQuery一樣選擇對(duì)象了
>>> doc('.pl')
[<span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span#rateword.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <span.pl>, <p.pl>]
這樣,class為'pl'的對(duì)象就全部選擇出來了。
不過在使用迭代時(shí)需要對(duì)文本進(jìn)行重新封裝:
for para in doc('.pl'):
para=pq(para)
print para.text()
導(dǎo)演
編劇
主演
類型:
官方網(wǎng)站:
制片國家/地區(qū):
語言:
上映日期:
片長:
IMDb鏈接:
官方小站:
這里得到的text是unicode碼,如果要寫入文件需要編碼為字符串。
用戶可以使用jquery提供的一些偽類(但還不支持css)來進(jìn)行操作,諸如:
>>> doc('.pl:first')
[<span.pl>]
>>> print doc('.pl:first').text()
導(dǎo)演
Attributes
獲取html元素的屬性
>>> p=pq('<p id="hello" class="hello"></p>')('p')
>>> p.attr('id')
'hello'
>>> p.attr.id
'hello'
>>> p.attr['id']
'hello'
賦值
>>> p.attr.id='plop'
>>> p.attr.id
'plop'
>>> p.attr['id']='ola'
>>> p.attr.id
'ola'
>>> p.attr(id='hello',class_='hello2')
[<p#hello.hell0>]
Traversing
過濾
>>> d=pq('<p id="hello" class="hello"><a/>hello</p><p id="test"><a/>world</p>')
>>> d('p').filter('.hello')
[<p#hello.hello>]
>>> d('p').filter('#test')
[<p#test>]
>>> d('p').filter(lambda i:i==1)
[<p#test>]
>>> d('p').filter(lambda i:i==0)
[<p#hello.hello>]
>>> d('p').filter(lambda i:pq(this).text()=='hello')
[<p#hello.hello>]
按照順序選擇
>>> d('p').eq(0)
[<p#hello.hello>]
>>> d('p').eq(1)
[<p#test>]
選擇內(nèi)嵌元素
>>> d('p').eq(1).find('a')
[<a>]
選擇父元素
>>> d=pq('<p><span><em>Whoah!</em></span></p><p><em> there</em></p>')
>>> d('p').eq(1).find('em')
[<em>]
>>> d('p').eq(1).find('em').end()
[<p>]
>>> d('p').eq(1).find('em').end().text()
'there'
>>> d('p').eq(1).find('em').end().end()
[<p>, <p>]
相關(guān)文章
python flask中動(dòng)態(tài)URL規(guī)則詳解
今天小編就為大家分享一篇python flask中動(dòng)態(tài)URL規(guī)則詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11Flask框架debug與配置項(xiàng)的開啟與設(shè)置詳解
這篇文章主要介紹了Flask框架debug與配置項(xiàng)的開啟與設(shè)置,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09解決TensorFlow模型恢復(fù)報(bào)錯(cuò)的問題
今天小編就為大家分享一篇解決TensorFlow模型恢復(fù)報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02Python編程中運(yùn)用閉包時(shí)所需要注意的一些地方
這篇文章主要介紹了Python編程中運(yùn)用閉包時(shí)所需要注意的一些地方,文章來自國內(nèi)知名的Python開發(fā)者felinx的博客,需要的朋友可以參考下2015-05-05Python字符串字母大小寫轉(zhuǎn)換的各種情況詳析
在使用python語言開發(fā)中經(jīng)常會(huì)碰到,需要大寫轉(zhuǎn)小寫,小寫轉(zhuǎn)換大寫,甚至字符串中的單詞首字母大寫,以及字符串手字字母大寫的問題,下面這篇文章主要給大家介紹了關(guān)于Python字符串字母大小寫轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2022-05-05python安裝dlib庫報(bào)錯(cuò)問題及解決方法
這篇文章主要介紹了python安裝dlib庫報(bào)錯(cuò)問題及解決方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03python自動(dòng)化測(cè)試selenium定位frame及iframe示例
這篇文章主要為大家介紹了python自動(dòng)化測(cè)試selenium定位frame及iframe示例的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11Python從csv文件中讀取數(shù)據(jù)及提取數(shù)據(jù)的方法
這篇文章主要介紹了Python從csv文件中讀取數(shù)據(jù)并提取數(shù)據(jù)的方法,文中通過多種方法給大家講解獲取指定列的數(shù)據(jù),并存入一個(gè)數(shù)組中,每種方法通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-11-11