- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- python中pyquery怎么用
小編給大家分享一下python中pyquery怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
使用pyquery需要在Web和了解jQuery的基礎上,使用該CSS選擇器。
使用pyquery初始化的方式有很多,傳入的參數可以是字符串,也可以是URL和文件名,下面將一一介紹初始化方法。
html = ''' <html> <head> <meta charset="utf-8"> <title>test02.html</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div id="container"> <iframe id="iframe" sandbox="allow-scripts" src="/files/%E7%88%AC%E8%99%AB%E5%86%99%E4%BD%9C%E4%BB%A3%E7%A0%81%E6%B5%8B%E8%AF%95/test02.html"></iframe> </div> </body> </html> ''' from pyquery import PyQuery as pq doc = pq(html) print(doc('title'))
【運行結果】
<title>test02.html</title>
URL以CSDN首頁(yè)地址為例:
from pyquery import PyQuery as pq doc = pq(url = 'https://www.csdn.net/') print(doc('title'))
【運行結果】
<title>CSDN - 專(zhuān)業(yè)開(kāi)發(fā)者社區</title>
我們將以下字符串保存為一個(gè)HTML文件,通過(guò)文件的形式進(jìn)行初始化。
【test02.html】
<bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book> </bookstore>
from pyquery import PyQuery as pq doc = pq(filename = 'test02.html') print(doc('title'))
【運行結果】
<title lang="eng">Harry Potter</title>
<title lang="eng">Learning XML</title>
查找子節點(diǎn)時(shí)需要用到find()方法,此時(shí)傳入的參數是CSS選擇器。
from pyquery import PyQuery as pq doc = pq(filename = 'test02.html') item = doc('book') print(item) lis1 = item.find('title') lis2 = item.find('price') print(lis1) print(lis2)
【運行結果】
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book><book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book><title lang="eng">Harry Potter</title>
<title lang="eng">Learning XML</title>
<price>29.99</price>
<price>39.95</price>
可以看到,我們首先匹配的是book節點(diǎn),然后匹配book節點(diǎn)下的子節點(diǎn)title和price。
其實(shí)使用find方法匹配的是所有的子孫節點(diǎn),如果只是單純匹配子節點(diǎn)可以使用children方法。
使用parent()方法,如果是要匹配祖先節點(diǎn),則需要使用parents()方法。
可以使用siblings()方法。
對于獲取到的內容如果是單個(gè)節點(diǎn),則可以直接轉換為字符串類(lèi)型,而對于獲取到多個(gè)節點(diǎn),因其類(lèi)型為PyQuery類(lèi)型,需要對獲取到的數據進(jìn)行遍歷,這是需要調用items()方法。
from pyquery import PyQuery as pq doc = pq(filename = 'test02.html') items = doc('title').items() print(items) print(type(items)) for i in items: print(type(i)) print(i)
【運行結果】
<generator object PyQuery.items at 0x000002B79E13EF48>
<class 'generator'>
<class 'pyquery.pyquery.PyQuery'>
<title lang="eng">Harry Potter</title>
<class 'pyquery.pyquery.PyQuery'>
<title lang="eng">Learning XML</title>
使用attr()方法
from pyquery import PyQuery as pq doc = pq(filename = 'test02.html') items = doc('title') for i in items.items(): print(i.attr('lang'))
【運行結果】
eng
eng
遍歷獲取到的數據,就能獲得所有title節點(diǎn)的land屬性值。
使用text()方法
from pyquery import PyQuery as pq doc = pq(filename = 'test02.html') items = doc('title') for i in items.items(): print(i.text())
【運行結果】
Harry Potter
Learning XML
同樣是遍歷,獲取到每一個(gè)title節點(diǎn)的文本值。
調用的方法為addClass和removeClass
from pyquery import PyQuery as pq doc = pq(filename = 'test02.html') items = doc('title') for i in items.items(): print(i) i.addClass('book01') print(i) i.removeClass('book01') print(i)
【運行結果】
<title lang="eng">Harry Potter</title>
<title lang="eng" class="book01">Harry Potter</title>
<title lang="eng" class="">Harry Potter</title>
<title lang="eng">Learning XML</title>
<title lang="eng" class="book01">Learning XML</title>
<title lang="eng" class="">Learning XML</title>
可以看到,首先是打印最初始的title節點(diǎn),加上class屬性后再次打印,去掉class屬性后再次打印。
attr:用來(lái)改變屬性值;
text:用來(lái)改變文本值;
html:用來(lái)改變節點(diǎn)值;
移除不需要的節點(diǎn)值,將整個(gè)節點(diǎn)移除。
支持多種偽類(lèi)選擇器,例如選擇第一個(gè)節點(diǎn)、最后一個(gè)節點(diǎn)、奇數節點(diǎn)、偶數節點(diǎn)、以及包含指定文本的節點(diǎn)等。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站