這篇文章給大家介紹PyQuery怎么在Python中使用,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
安裝
PyQuery庫官方文檔
初始化為PyQuery對(duì)象
常用的CCS選擇器
偽類選擇器
查找標(biāo)簽
獲取標(biāo)簽信息
初始化為PyQuery對(duì)象
html = """ <html lang="en"> <head> 簡(jiǎn)單好用的 <title>PyQuery</title> </head> <body> <ul id="container"> <li class="object-1">Python</li> <li class="object-2">大法</li> <li class="object-3">好</li> </ul> </body> </html> """
相當(dāng)于BeautifulSoup庫的初識(shí)化方法,將html轉(zhuǎn)化為BeautifulSoup對(duì)象。
bsObj = BeautifulSoup(html, 'html.parser')
PyQuery庫也要有自己的初始化。
1.1 將字符串初始化
from pyquery import PyQuery as pq #初始化為PyQuery對(duì)象 doc = pq(html) print(type(doc)) print(doc)
返回
<class 'pyquery.pyquery.PyQuery'> <html lang="en"> <head> <title>PyQuery學(xué)習(xí)</title> </head> <body> <ul id="container"> <li class="object-1"/> <li class="object-2"/> <li class="object-3"/> </ul> </body> </html>
1.2 將html文件初始化
#filename參數(shù)為html文件路徑 test_html = pq(filename = 'test.html') print(type(test_html)) print(test_html)
返回
<class 'pyquery.pyquery.PyQuery'><html lang="en"> <head> <title>PyQuery學(xué)習(xí)</title> </head> <body> <ul id="container"> <li class="object-1"/> <li class="object-2"/> <li class="object-3"/> </ul> </body> </html>
1.3 對(duì)網(wǎng)址響應(yīng)進(jìn)行初始化
response = pq(url = 'https://www.baidu.com') print(type(response)) print(response)
返回
<class 'pyquery.pyquery.PyQuery'> <html> <head><meta http-equiv="content-type" content="text/html;charset=utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=Edge"/><meta content="always" name="referrer"/><link rel="stylesheet" type="text/css" href="https://cache.yisu.com/upload/information/20200622/113/62531.html" width="270" height="129"/> </div> <form id="form" name="f" action="//www.baidu.com/s" class="fm"> <input type="hidden" name="bdorz_come" value="1"/> <input type="hidden" name="ie" value="utf-8"/> <input type="hidden" name="f" value="8"/> <input type="hidden" name="rsv_bp" value="1"/> <input type="hidden" name="rsv_idx" value="1"/> <input type="hidden" name="tn" value="baidu"/><span class="bg s_ipt_wr"><input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off" autofocus="autofocus"/></span><span class="bg s_btn_wr"><input type="submit" id="su" value="ç?¾åº¦ä¸?ä¸?" class="bg s_btn" autofocus=""/></span> </form> </div> </div> <div id="u1"> <a href="http://news.baidu.com" rel="external nofollow" name="tj_trnews" class="mnav">æ?°é?»</a> <a href="https://www.hao123.com" rel="external nofollow" name="tj_trhao123" class="mnav">hao123</a> <a href="http://map.baidu.com" rel="external nofollow" name="tj_trmap" class="mnav">å?°å?¾</a> <a href="http://v.baidu.com" rel="external nofollow" name="tj_trvideo" class="mnav">è§?é¢?</a> <a href="http://tieba.baidu.com" rel="external nofollow" name="tj_trtieba" class="mnav">è´´å?§</a> <noscript> <a href="https://cache.yisu.com/upload/information/20200622/113/62532.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1" rel="external nofollow" name="tj_login" class="lb">ç?»å½?</a> </noscript> <script>document.write('<a href="https://cache.yisu.com/upload/information/20200622/113/62532.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === " rel="external nofollow" " ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">ç?»å½?</a>'); </script> <a href="//www.baidu.com/more/" rel="external nofollow" name="tj_briicon" class="bri" >æ?´å¤?产å??</a> </div> </div> </div> <div id="ftCon"> <div id="ftConw"> <p id="lh"> <a href="http://home.baidu.com" rel="external nofollow" >å ³äº?ç?¾åº¦</a> <a href="http://ir.baidu.com" rel="external nofollow" >About Baidu</a> </p> <p id="cp">©2017 Baidu <a href="http://www.baidu.com/duty/" rel="external nofollow" >使ç?¨ç?¾åº¦å??å¿ è¯»</a> <a href="https://cache.yisu.com/upload/information/20200622/113/62533.html"/> </p> </div> </div> </div> </body> </html>
二、常用的CCS選擇器
打印id為container的標(biāo)簽
print(doc('#container')) print(type(doc('#container')))
返回
<ul id="container"> <li class="object-1"/> <li class="object-2"/> <li class="object-3"/> </ul> <class 'pyquery.pyquery.PyQuery'>
打印class為object-1的標(biāo)簽
print(doc('.object-1'))
返回
<li class="object-1"/>
打印標(biāo)簽名為body的標(biāo)簽
print(doc('body'))
返回
<body> <ul id="container"> <li class="object-1"/> <li class="object-2"/> <li class="object-3"/> </ul> </body>
多種css選擇器使用
print(doc('html #container'))
返回
<ul id="container"> <li class="object-1"/> <li class="object-2"/> <li class="object-3"/> </ul>
三、偽類選擇器
偽類nth
print(pseudo_doc('li:nth-child(2)')) #打印第一個(gè)li標(biāo)簽 print(pseudo_doc('li:first-child')) #打印最后一個(gè)標(biāo)簽 print(pseudo_doc('li:last-child'))
返回
<li class="object-2">大法</li> <li class="object-1">Python</li> <li class="object-6">好玩</li>
contains
#找到含有Python的li標(biāo)簽 print(pseudo_doc("li:contains('Python')")) #找到含有好的li標(biāo)簽 print(pseudo_doc("li:contains('好')"))
返回
<li class="object-1">Python</li> <li class="object-3">好</li> <li class="object-4">好</li> <li class="object-6">好玩</li>
四、查找標(biāo)簽
按照條件在Pyquery對(duì)象中查找符合條件的標(biāo)簽,類似于BeautifulSoup中的find方法。
打印id=container的標(biāo)簽
print(doc.find('#container'))
返回
<ul id="container"> <li class="object-1"/> <li class="object-2"/> <li class="object-3"/> </ul> print(doc.find('li'))
返回
<li class="object-1"/> <li class="object-2"/> <li class="object-3"/>
4.2 子輩標(biāo)簽-children方法
#id=container的標(biāo)簽的子輩標(biāo)簽 container = doc.find('#container') print(container.children())
返回
<li class="object-1"/> <li class="object-2"/> <li class="object-3"/>
4.3 父輩標(biāo)簽-parent方法
object_2 = doc.find('.object-2') print(object_2.parent())
返回
<ul id="container"> <li class="object-1"/> <li class="object-2"/> <li class="object-3"/> </ul>
4.4 兄弟標(biāo)簽-siblings方法
object_2 = doc.find('.object-2') print(object_2.siblings())
返回
<li class="object-1"/> <li class="object-3"/>
五、獲取標(biāo)簽的信息
定位到目標(biāo)標(biāo)簽后,我們需要標(biāo)簽內(nèi)部的文本或者屬性值,這時(shí)候需要進(jìn)行提取文本或?qū)傩灾挡僮?/p>
5.1 標(biāo)簽屬性值的提取
.attr() 傳入 標(biāo)簽的屬性名,返回屬性值
object_2 = doc.find('.object-2') print(object_2.attr('class'))
返回
object-2
5.2 標(biāo)簽內(nèi)的文本
.text()
html_text = """ <html lang="en"> <head> 簡(jiǎn)單好用的 <title>PyQuery</title> </head> <body> <ul id="container"> Hello World! <li class="object-1">Python</li> <li class="object-2">大法</li> <li class="object-3">好</li> </ul> </body> </html> """ docs = pq(html_text) print(docs.text())
返回
簡(jiǎn)單好用的 PyQuery Hello World! Python 大法 好
object_1 = docs.find('.object-1') print(object_1.text()) container = docs.find('#container') print(container.text())
返回
Python Hello World! Python 大法 好
tips:如果我只想獲得Hello World這個(gè),不想得到其他的文本,可以使用remove方法將li標(biāo)簽去掉,然后再使用text方法
container = docs.find('#container') container.remove('li') print(container.text())
返回
Hello World!
pyquery一些自定義的用法
訪問網(wǎng)址
PyQuery與BeautifulSoup對(duì)比,我們會(huì)發(fā)現(xiàn)PyQuery可以對(duì)網(wǎng)址發(fā)起請(qǐng)求。 比如
from pyquery import PyQuery PyQuery(url = 'https://www.baidu.com')
opener參數(shù)
這是PyQuery對(duì)百度網(wǎng)址進(jìn)行請(qǐng)求,并將請(qǐng)求返回的響應(yīng)數(shù)據(jù)處理為PyQuery對(duì)象。一般pyquery庫會(huì)默認(rèn)調(diào)用urllib庫,如果想使用selenium或者requests庫,可以自定義PyQuery的opener參數(shù)。
opener參數(shù)作用是告訴pyquery用什么請(qǐng)求庫對(duì)網(wǎng)址發(fā)起請(qǐng)求。常見的請(qǐng)求庫如urllib、requests、selenium。這里我們自定義一個(gè)selenium的opener。
from pyquery import PyQuery from selenium.webdriver import PhantomJS #用selenium訪問url def selenium_opener(url): #我沒有將Phantomjs放到環(huán)境變量,所以每次用都要放上路徑 driver = PhantomJS(executable_path = 'phantomjs的路徑') driver.get(url) html = driver.page_source driver.quit() return html #注意,使用時(shí)opener參數(shù)是函數(shù)名,沒有括號(hào)的! PyQuery(url='https://www.baidu.com/', opener=selenium_opener)
這時(shí)候我們就能對(duì)PyQuery對(duì)象進(jìn)行操作,提取有用的信息。具體請(qǐng)看上次的分享,如果想了解更多的功能,pyquery文檔寫的不怎么詳細(xì),好在基本跟jQuery功能吻合,我們?nèi)绻胗煤胮yquery,需要查看jQuery文檔。
cookies、headers
在requests用法中,一般為了訪問網(wǎng)址更加真實(shí),模仿成瀏覽器。一般我們需要傳入headers,必要的時(shí)候還需要傳入cookies參數(shù)。而pyquery庫就有這功能,也能偽裝瀏覽器。
from pyquery import PyQuery cookies = {'Cookie':'你的cookie'} headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'} PyQuery(url='https://www.baidu.com/',headers=headers,cookies=cookies)
讓你的selenium帶上pyquery功能
讓driver訪問的網(wǎng)址得到的網(wǎng)頁直接變?yōu)镻yQuery對(duì)象,更方便提取數(shù)據(jù)
from pyquery import PyQuery from selenium.webdriver import PhantomJS class Browser(PhantomJS): @property def dom(self): return PyQuery(self.page_source)""" 這部分property是裝飾器,需要知道@property下面緊跟的函數(shù),實(shí)現(xiàn)了類的屬性功能。 這里browser.dom,就是browser的dom屬性。 """ browser = Browser(executable_path='phantomjs的路徑') browser.get(url='https://www.baidu.com/') print(type(browser.dom))
返回
<class 'pyquery.pyquery.PyQuery'>
關(guān)于PyQuery怎么在Python中使用就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)頁名稱:PyQuery怎么在Python中使用-創(chuàng)新互聯(lián)
地址分享:http://jinyejixie.com/article20/discjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站營(yíng)銷、做網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容