最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python解析html開發(fā)庫pyquery使用方法

 更新時間:2014年02月07日 09:59:11   投稿:zxhpj  
PyQuery是一個類似于jQuery的Python庫,也可以說是jQuery在Python上的實(shí)現(xiàn),能夠以jQuery的語法來操作解析 HTML 文檔,易用性和解析速度都很好

例如

復(fù)制代碼 代碼如下:

<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>

復(fù)制代碼 代碼如下:

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()

輸出

復(fù)制代碼 代碼如下:

導(dǎo)演
編劇
主演
類型:
官方網(wǎng)站:
制片國家/地區(qū):
語言:
上映日期:
片長:
IMDb鏈接:
官方小站:

用法

用戶可以使用PyQuery類從字符串、lxml對象、文件或者url來加載xml文檔:

復(fù)制代碼 代碼如下:

>>> 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一樣選擇對象了

復(fù)制代碼 代碼如下:

>>> 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'的對象就全部選擇出來了。

不過在使用迭代時需要對文本進(jìn)行重新封裝:

復(fù)制代碼 代碼如下:

for para in doc('.pl'):
    para=pq(para)
    print para.text()  
導(dǎo)演
編劇
主演
類型:
官方網(wǎng)站:
制片國家/地區(qū):
語言:
上映日期:
片長:
IMDb鏈接:
官方小站:

這里得到的text是unicode碼,如果要寫入文件需要編碼為字符串。
用戶可以使用jquery提供的一些偽類(但還不支持css)來進(jìn)行操作,諸如:

復(fù)制代碼 代碼如下:

>>> doc('.pl:first')
[<span.pl>]
>>> print  doc('.pl:first').text()
導(dǎo)演

Attributes
獲取html元素的屬性

復(fù)制代碼 代碼如下:

>>> p=pq('<p id="hello" class="hello"></p>')('p')
>>> p.attr('id')
'hello'
>>> p.attr.id
'hello'
>>> p.attr['id']
'hello'

賦值

復(fù)制代碼 代碼如下:

>>> 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
過濾

復(fù)制代碼 代碼如下:

>>> 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>]

按照順序選擇

復(fù)制代碼 代碼如下:

>>> d('p').eq(0)
[<p#hello.hello>]
>>> d('p').eq(1)
[<p#test>]

選擇內(nèi)嵌元素

復(fù)制代碼 代碼如下:

>>> d('p').eq(1).find('a')
[<a>]

選擇父元素

復(fù)制代碼 代碼如下:

>>> 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)文章

  • certifi輕松地管理Python證書信任鏈保障網(wǎng)絡(luò)安全

    certifi輕松地管理Python證書信任鏈保障網(wǎng)絡(luò)安全

    在使用Python進(jìn)行網(wǎng)絡(luò)通信時,我們通常需要使用第三方庫來處理HTTPS連接,其中,certifi庫是一個非常實(shí)用的庫,可以幫助我們輕松地管理Python的證書信任鏈
    2024-01-01
  • Python Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法匯總

    Python Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法匯總

    這篇文章主要介紹了Python中的Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法,創(chuàng)建Dataframe主要是使用pandas中的DataFrame函數(shù),其核心就是第一個參數(shù):data,傳入原始數(shù)據(jù),因此我們可以據(jù)此給出六種創(chuàng)建Dataframe的方法,需要的朋友可以參考下
    2023-05-05
  • Python中 CSV格式清洗與轉(zhuǎn)換的實(shí)例代碼

    Python中 CSV格式清洗與轉(zhuǎn)換的實(shí)例代碼

    這篇文章主要介紹了Python123 CSV格式清洗與轉(zhuǎn)換的實(shí)例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • python中執(zhí)行shell的兩種方法總結(jié)

    python中執(zhí)行shell的兩種方法總結(jié)

    這篇文章主要介紹了python中執(zhí)行shell的兩種方法,有兩種方法可以在Python中執(zhí)行SHELL程序,方法一是使用Python的commands包,方法二則是使用subprocess包,這兩個包均是Python現(xiàn)有的內(nèi)置模塊。需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • 在python 中實(shí)現(xiàn)運(yùn)行多條shell命令

    在python 中實(shí)現(xiàn)運(yùn)行多條shell命令

    今天小編就為大家分享一篇在python 中實(shí)現(xiàn)運(yùn)行多條shell命令,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python判斷文件和字符串編碼類型的實(shí)例

    Python判斷文件和字符串編碼類型的實(shí)例

    下面小編就為大家分享一篇Python判斷文件和字符串編碼類型的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • python實(shí)現(xiàn)識別相似圖片小結(jié)

    python實(shí)現(xiàn)識別相似圖片小結(jié)

    本文給大家分享的是使用Python實(shí)現(xiàn)圖片相似度識別的總結(jié),代碼實(shí)用pil模塊比較兩個圖片的相似度,根據(jù)實(shí)際實(shí)用,代碼雖短但效果不錯,還是非??孔V的。
    2016-02-02
  • TensorFlow2.0使用keras訓(xùn)練模型的實(shí)現(xiàn)

    TensorFlow2.0使用keras訓(xùn)練模型的實(shí)現(xiàn)

    這篇文章主要介紹了TensorFlow2.0使用keras訓(xùn)練模型的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 最新評論

    平武县| 叶城县| 漳州市| 栾城县| 新安县| 博客| 崇明县| 枝江市| 荔波县| 金坛市| 建昌县| 清苑县| 弥勒县| 兴仁县| 堆龙德庆县| 惠来县| 宁远县| 榆树市| 广汉市| 惠来县| 博野县| 万盛区| 沁水县| 阿坝县| 武安市| 霍山县| 沙田区| 杭锦旗| 郓城县| 沁源县| 克东县| 当阳市| 苍溪县| 千阳县| 平顺县| 长沙县| 中江县| 凤山县| 繁峙县| 鲜城| 灯塔市|