如何在python3中使用pyquery庫-創(chuàng)新互聯(lián)

本篇文章給大家分享的是有關(guān)如何在python3中使用pyquery庫,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

阿拉善盟網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運(yùn)營維護(hù)。成都創(chuàng)新互聯(lián)從2013年開始到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運(yùn)維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)

python的五大特點是什么

python的五大特點:1.簡單易學(xué),開發(fā)程序時,專注的是解決問題,而不是搞明白語言本身。2.面向?qū)ο?,與其他主要的語言如C++和Java相比, Python以一種非常強(qiáng)大又簡單的方式實現(xiàn)面向?qū)ο缶幊獭?.可移植性,Python程序無需修改就可以在各種平臺上運(yùn)行。4.解釋性,Python語言寫的程序不需要編譯成二進(jìn)制代碼,可以直接從源代碼運(yùn)行程序。5.開源,Python是 FLOSS(自由/開放源碼軟件)之一。

1、pyquery安裝

pip方式安裝:

$pip install pyquery

#它依賴cssselect和lxml包
pyquery==1.4.0
 - cssselect [required: >0.7.9, installed: 1.0.3] #CSS選擇器并將它轉(zhuǎn)換為XPath表達(dá)式
 - lxml [required: >=2.1, installed: 4.2.2] #處理xml和html解析庫

驗證安裝:

In [1]: import pyquery

In [2]: pyquery.text
Out[2]: <module 'pyquery.text' from '/root/pp1/.venv/lib/python3.6/site-packages/pyquery/text.py'>

2、pyquery對象初始化

pyquery首先需要傳入HTML文本來初始化一個pyquery對象,它的初始化方式有多種,如直接傳入字符串,傳入URL或者傳入文件名

(1)字符串初始化

from pyquery import PyQuery as pq

html='''
<div id="wenzhangziti" class="article 389862"><p>人生是一條沒有盡頭的路,不要留戀逝去的夢,把命運(yùn)掌握在自己手中,讓我們來掌握自己的命運(yùn),別讓別人的干擾與誘惑,別讓功名與利祿,來打翻我們這壇陳釀已久的命運(yùn)之酒!</p>
</div>
'''
doc=pq(html) #初始化并創(chuàng)建pyquery對象
print(type(doc))
print(doc('p').text())

#
<class 'pyquery.pyquery.PyQuery'>
人生是一條沒有盡頭的路,不要留戀逝去的夢,把命運(yùn)掌握在自己手中,讓我們來掌握自己的命運(yùn),別讓別人的干擾與誘惑,別讓功名與利祿,來打翻我們這壇陳釀已久的命運(yùn)之酒!

(2)URL初始化

from pyquery import PyQuery as pq

doc=pq(url='https://www.cnblogs.com/zhangxinqi/p/9218395.html')
print(type(doc))
print(doc('title'))

#
<class 'pyquery.pyquery.PyQuery'>
<title>python3解析庫BeautifulSoup4 - Py.qi - 博客園</title>&#13;

PyQuery能夠從url加載一個html文檔,之際上是默認(rèn)情況下調(diào)用python的urllib庫去請求響應(yīng),如果requests已安裝的話它將使用requests來請求響應(yīng),那我們就可以使用request的請求參數(shù)來構(gòu)造請求了,實際請求如下:

from pyquery import PyQuery as pq
import requests

doc=pq(requests.get(url='https://www.cnblogs.com/zhangxinqi/p/9218395.html').text)
print(type(doc))
print(doc('title'))

#輸出同上一樣
<class 'pyquery.pyquery.PyQuery'>
<title>python3解析庫BeautifulSoup4 - Py.qi - 博客園</title>&#13;

(3)通過文件初始化

通過本地的HTML文件來構(gòu)造PyQuery對象

from pyquery import PyQuery as pq

doc=pq(filename='demo.html',parser='html')
#doc=pq(open('demo.html','r',encoding='utf-8').read(),parser='html') #注意:在讀取有中文的HTML文件時,請使用此方法,否則會報解碼錯誤
print(type(doc))
print(doc('p'))

3、CSS選擇器

在使用屬性選擇器中,使用屬性選擇特定的標(biāo)簽,標(biāo)簽和CSS標(biāo)識必須引用為字符串,它會過濾篩選符合條件的節(jié)點打印輸出,返回的是一個PyQuery類型對象

from pyquery import PyQuery as pq
import requests
html='''
<div id="container">
 <ul class="list">
  <li class="item-0">first item</li>
  <li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li>
  <li class="item-0 active"><a href="link3.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span class="bold">third item</span></a></li>
  <li class="item-1 active"><a href="link4.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fourth item</a></li>
  <li class="item-0"><a href="link5.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fifth item</a></li>
 </ul>
 </div>
'''
doc=pq(html,parser='html')
print(doc('#container .list .item-0 a'))
print(doc('.list .item-1'))

#
<a href="link3.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span class="bold">third item</span></a><a href="link5.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fifth item</a>
<li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li>
  <li class="item-1 active"><a href="link4.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fourth item</a></li>

4、查找節(jié)點

PyQuery使用查詢函數(shù)來查詢節(jié)點,同jQuery中的函數(shù)用法完全相同

(1)查找子節(jié)點和子孫節(jié)點

使用find()方法獲取子孫節(jié)點,children()獲取子節(jié)點,使用以上的HTML代碼測試

from pyquery import PyQuery as pq
import requests

doc=pq(html,parser='html')
print('find:',doc.find('a'))
print('children:',doc('li').children('a'))

(2)獲取父節(jié)點和祖先節(jié)點

parent()方法獲取父節(jié)點,parents()獲取祖先節(jié)點

doc(.list).parent()
doc(.list).parents()

(3)獲取兄弟節(jié)點

siblings()方法用來獲取兄弟節(jié)點,可以嵌套使用,傳入CSS選擇器即可繼續(xù)匹配

doc('.list .item-0 .active').siblings('.active')

5、遍歷

對于pyquery的選擇結(jié)果可能是多個字節(jié),也可能是單個節(jié)點,類型都是PyQuery類型,它沒有返回列表等形式,對于當(dāng)個節(jié)點我們可指直接打印輸出或者直接轉(zhuǎn)換成字符串,而對于多個節(jié)點的結(jié)果,我們需要遍歷來獲取所有節(jié)點可以使用items()方法,它會返回一個生成器,循環(huán)得到的每個節(jié)點類型依然是PyQuery類型,所以我們可以繼續(xù)方法來選擇節(jié)點或?qū)傩?,?nèi)容等

lis=doc('li').items()
for i in lis:
 print(i('a')) #繼續(xù)獲取節(jié)點下的子節(jié)點

6、獲取信息

attr()方法用來獲取屬性,如返回的結(jié)果有多個時可以調(diào)用items()方法來遍歷獲取

doc('.item-0.active a').attr('href') #多屬性值中間不能有空格

text()方法用來獲取文本內(nèi)容,它只返回內(nèi)部的文本信息不包括HTML文本內(nèi)容,如果想返回包括HTML的文本內(nèi)容可以使用html()方法,如果結(jié)果有多個,text()方法會方法所有節(jié)點的文本信息內(nèi)容并將它們拼接用空格分開返回字符串內(nèi)容,html()方法只會返回第一個節(jié)點的HTML文本,如果要獲取所有就需要使用items()方法來遍歷獲取了

from pyquery import PyQuery as pq
html='''
<div id="container">
 <ul class="list">
   <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>
'''
doc=pq(html,parser='html')
print('text:',doc('li').text()) #獲取li節(jié)點下的所有文本信息
lis=doc('li').items()
for i in lis:
 print('html:',i.html()) #獲取所有l(wèi)i節(jié)點下的HTML文本

#
text: first item second item third item fourth item fifth item
html: first item
html: <a href="link2.html">second item</a>
html: <a href="link3.html"><span class="bold">third item</span></a>
html: <a href="link4.html">fourth item</a>
html: <a href="link5.html">fifth item</a>

7、節(jié)點操作

pyquery提供了一系列方法來對節(jié)點進(jìn)行動態(tài)修改,如添加一個class,移除某個節(jié)點,修改某個屬性的值

addClass()增加Class,removeClass()刪除Class

attr()增加屬性和值,text()增加文本內(nèi)容,html()增加HTML文本,remove()移除

from pyquery import PyQuery as pq
import requests
html='''
<div id="container">
 <ul class="list">
   <li id="1">first item</li>
   <li class="item-1"><a href="link2.html">second item</a></li>
   <li class="item-2 active"><a href="link3.html"><span class="bold">third item</span></a></li>
   <li class="item-3 active"><a href="link4.html">fourth item</a></li>
   <li class="item-4"><a href="link5.html">fifth item</a></li>
  </ul>
 </div>
'''
doc=pq(html,parser='html')
print(doc('#1'))
print(doc('#1').add_class('myclass')) #增加Class
print(doc('.item-1').remove_class('item-1')) #刪除Class
print(doc('#1').attr('name','link')) #添加屬性name=link
print(doc('#1').text('hello world')) #添加文本
print(doc('#1').html('<span>changed item</span>')) #添加HTML文本
print(doc('.item-2.active a').remove('span')) #刪除節(jié)點

#
<li id="1">first item</li>
   
<li id="1" class="myclass">first item</li>
   
<li class=""><a href="link2.html">second item</a></li>
   
<li id="1" class="myclass" name="link">first item</li>
   
<li id="1" class="myclass" name="link">hello world</li>
   
<li id="1" class="myclass" name="link"><span>changed item</span></li>
<a href="link3.html"/>

after()在節(jié)點后添加值

before()在節(jié)點之前插入值

append()將值添加到每個節(jié)點

contents()返回文本節(jié)點內(nèi)容

empty()刪除節(jié)點內(nèi)容

remove_attr()刪除屬性

val()設(shè)置或獲取屬性值

另外還有很多節(jié)點操作方法,它們和jQuery的用法完全一致,詳細(xì)請參考:http://pyquery.readthedocs.io/en/latest/api.html

8、偽類選擇器

CSS選擇器之所以強(qiáng)大,是因為它支持多種多樣的偽類選擇器,如:選擇第一個節(jié)點,最后一個節(jié)點,奇偶數(shù)節(jié)點等。

#!/usr/bin/env python
#coding:utf-8
from pyquery import PyQuery as pq

html='''
<div id="container">
 <ul class="list">
   <li id="1">first item</li>
   <li class="item-1"><a href="link2.html">second item</a></li>
   <li class="item-2 active"><a href="link3.html"><span class="bold">third item</span></a></li>
   <li class="item-3 active"><a href="link4.html">fourth item</a></li>
   <li class="item-4"><a href="link5.html">fifth item</a></li>
  </ul>
  <div><input type="text" value="username"/></div> 
</div>
'''
doc=pq(html,parser='html')
print('第一個li節(jié)點:',doc('li:first-child')) #第一個li節(jié)點
print('最后一個li節(jié)點:',doc('li:last_child')) #最后一個li節(jié)點
print('第二個li節(jié)點:',doc('li:nth-child(2)')) #第二個li節(jié)點
print('第三個之后的所有l(wèi)i節(jié)點:',doc('li:gt(2)')) #第三個之后的所有l(wèi)i節(jié)點
print('偶數(shù)的所有l(wèi)i節(jié)點:',doc('li:nth-child(2n)')) #偶數(shù)的所有l(wèi)i節(jié)點
print('包含文本內(nèi)容的節(jié)點:',doc('li:contains(second)')) #包含文本內(nèi)容的節(jié)點
print('索引第一個節(jié)點:',doc('li:eq(0)'))
print('奇數(shù)節(jié)點:',doc('li:even'))
print('偶數(shù)節(jié)點:',doc('li:odd'))

#
第一個li節(jié)點: <li id="1">first item</li>
   
最后一個li節(jié)點: <li class="item-4"><a href="link5.html">fifth item</a></li>
  
第二個li節(jié)點: <li class="item-1"><a href="link2.html">second item</a></li>
   
第三個之后的所有l(wèi)i節(jié)點: <li class="item-3 active"><a href="link4.html">fourth item</a></li>
   <li class="item-4"><a href="link5.html">fifth item</a></li>
  
偶數(shù)的所有l(wèi)i節(jié)點: <li class="item-1"><a href="link2.html">second item</a></li>
   <li class="item-3 active"><a href="link4.html">fourth item</a></li>
   
包含文本內(nèi)容的節(jié)點: <li class="item-1"><a href="link2.html">second item</a></li>
   
索引第一個節(jié)點: <li id="1">first item</li>
   
奇數(shù)節(jié)點: <li id="1">first item</li>
   <li class="item-2 active"><a href="link3.html"><span class="bold">third item</span></a></li>
   <li class="item-4"><a href="link5.html">fifth item</a></li>
  
偶數(shù)節(jié)點: <li class="item-1"><a href="link2.html">second item</a></li>
   <li class="item-3 active"><a href="link4.html">fourth item</a></li>

9、實例應(yīng)用

 抓取http://www.mzitu.com網(wǎng)站美女圖片12萬張用時28分鐘,總大小9G,主要受網(wǎng)絡(luò)帶寬影響,下載數(shù)據(jù)有點慢

#!/usr/bin/env python
#coding:utf-8
import requests
from requests.exceptions import RequestException
from pyquery import PyQuery as pq
from PIL import Image
from PIL import ImageFile
from io import BytesIO
import time
from multiprocessing import Pool,freeze_support
ImageFile.LOAD_TRUNCATED_IMAGES = True

headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'
,'Referer':'http://www.mzitu.com'
}

img_headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'
,'Referer':'http://i.meizitu.net'
}
#保持會話請求
sesion=requests.Session()

#獲取首頁所有URL并返回列表
def get_url(url):
 list_url = []
 try:
  html=sesion.get(url,headers=headers).text
  doc=pq(html,parser='html')
  url_path=doc('#pins > li').children('a')
  for i in url_path.items():
   list_url.append(i.attr('href'))
 except RequestException as e:
  print('get_url_RequestException:',e)
 except Exception as e:
  print('get_url_Exception:',e)
 return list_url

#組合首頁中每個地址的圖片分頁返回列表
def list_get_pages(list_url):
 list_url_fen=[]
 try:
  for i in list_url:
   doc_children = pq(sesion.get(i,headers=headers).text,parser='html')
   img_number = doc_children('body > div.main > div.content > div.pagenavi > a:nth-child(7) > span').text()
   number=int(img_number.strip())
   for j in range(1,number+1):
    list_url_fen.append(i+'/'+str(j))
 except ValueError as e:
  print('list_get_pages_ValueError:',e)
 except RequestException as e:
  print('list_get_pages_RequestException',e)
 except Exception as e:
  print('list_get_pages_Exception:',e)
 return list_url_fen

#獲取image地址并下載圖片
def get_image(url):
 im_path=''
 try:
  html=sesion.get(url, headers=headers).text
  doc=pq(html,parser='html')
  im_path=doc('.main-image a img').attr('src')
  image_names = ''.join(im_path.split('/')[-3:])
  image_path = 'D:\images\\' + image_names
  with open('img_url.txt','a') as f:
   f.write(im_path + '\n')
  r=requests.get(im_path,headers=img_headers)
  b=BytesIO(r.content)
  i=Image.open(b)
  i.save(image_path)
  b.close()
  i.close()
  #print('下載圖片:{}成功!'.format(image_names))
 except RequestException as e:
  print('RequestException:',e)
 except OSError as e:
  print('OSError:',e)
 except Exception as e: #必須捕獲所有異常,運(yùn)行中有一些鏈接地址不符合抓取規(guī)律,需要捕獲異常使程序正常運(yùn)行
  print('Exception:',e)
 return im_path


#主調(diào)用函數(shù)
def main(item):
 url1='http://www.mzitu.com/page/{}'.format(item) #分頁地址
 print('開始下載地址:{}'.format(url1))
 獲取首頁鏈接地址
 html=get_url(url1)
 #獲取分頁鏈接地址
 list_fenurl = list_get_pages(html)
 #根據(jù)分頁鏈接地址獲取圖片并下載
 for i in list_fenurl:
  get_image(i)
 return len(list_fenurl) #統(tǒng)計下載數(shù)

if __name__ == '__main__':
 freeze_support() #windows下進(jìn)程調(diào)用時必須添加
 pool=Pool() #創(chuàng)建進(jìn)程池
 start=time.time()
 count=pool.map(main,[i for i in range(1,185)]) #多進(jìn)程運(yùn)行翻頁主頁
 print(sum(count),count) #獲取總的下載數(shù)
 end=time.time()
 data=time.strftime('%M:%S',time.localtime(end-start)) #獲取程序運(yùn)行時間
 print('程序運(yùn)行時間:{}分{}秒'.format(*data.split(':')))

#學(xué)習(xí)階段,代碼寫得通用性很差,以后改進(jìn)!
#運(yùn)行結(jié)果
#會有幾個報錯都忽略了是獲取文件名時的分割問題和在圖片很少的情況下導(dǎo)致獲取不到單分頁圖片的數(shù)目,先忽略以后有時間再改正
#Exception: 'NoneType' object has no attribute 'split'
#list_get_pages_ValueError: invalid literal for int() with base 10: '下一頁&raquo;'

開始下載地址:http://www.mzitu.com/page/137
OSError: image file is truncated (22 bytes not processed)
開始下載地址:http://www.mzitu.com/page/138

程序運(yùn)行時間:28分27秒

進(jìn)程完成,退出碼 0

以上就是如何在python3中使用pyquery庫,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

文章題目:如何在python3中使用pyquery庫-創(chuàng)新互聯(lián)
轉(zhuǎn)載來源:http://muchs.cn/article48/deephp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、標(biāo)簽優(yōu)化響應(yīng)式網(wǎng)站、微信小程序搜索引擎優(yōu)化、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)