python爬蟲(chóng)學(xué)習(xí)筆記之pyquery模塊基本用法詳解
本文實(shí)例講述了python爬蟲(chóng)學(xué)習(xí)筆記之pyquery模塊基本用法。分享給大家供大家參考,具體如下:
相關(guān)內(nèi)容:
- pyquery的介紹
- pyquery的使用
- 安裝模塊
- 導(dǎo)入模塊
- 解析對(duì)象初始化
- css選擇器
- 在選定元素之后的元素再選取
- 元素的文本、屬性等內(nèi)容的獲取
- pyquery執(zhí)行DOM操作、css操作
- Dom操作
- CSS操作
- 一個(gè)利用pyquery爬取豆瓣新書(shū)的例子
首發(fā)時(shí)間:2018-03-09 21:26
pyquery的介紹
- pyquery允許對(duì)xml、html文檔進(jìn)行jQuery查詢。
- pyquery使用lxml進(jìn)行快速xml和html操作。
- pyquery是python中的jquery
PyQuery的使用:
1.安裝模塊:
pip3 install pyquery
2.導(dǎo)入模塊:
from pyquery import PyQuery as pq
3.解析對(duì)象初始化:
【使用PyQuery初始化解析對(duì)象,PyQuery是一個(gè)類,直接將要解析的對(duì)象作為參數(shù)傳入即可】
- 解析對(duì)象為字符串時(shí)字符串初始化 :默認(rèn)情況下是字符串,如果字符串是一個(gè)帶http\https前綴的,將會(huì)認(rèn)為是一個(gè)url
textParse = pq(html)
- 解析對(duì)象為網(wǎng)頁(yè)時(shí)url初始化: 建議使用關(guān)鍵字參數(shù)url=
# urlParse = pq('http://www.baidu.com') #1 urlParse = pq(url='http://www.baidu.com') #2 - 解析對(duì)象為文件時(shí)文件初始化:建議使用關(guān)鍵字參數(shù)filename=
fileParse = pq(filename="L:\demo.html")
- 解析完畢后,就可以使用相關(guān)函數(shù)或變量來(lái)進(jìn)行篩選,可以使用css等來(lái)篩選,
4.CSS選擇器:
- 利用標(biāo)簽獲取:
result = textParse('h2').text() - 利用類選擇器:
result3=textParse(".p1").text() - 利用id選擇:
result4=textParse("#user").attr("type") - 分組選擇:
result5=textParse("p,div").text() - 后代選擇器:
result6=textParse("div a").attr.href - 屬性選擇器:
result7=textParse("[class='p1']").text() - CSS3偽類選擇器:
result8=textParse("p:last").text()
(更多的,可以參考css)
5.在選定元素之后的元素再選取:
- find():找出指定子元素 ,find可以有參數(shù),該參數(shù)可以是任何 jQuery 選擇器的語(yǔ)法,
- filter():對(duì)結(jié)果進(jìn)行過(guò)濾,找出指定元素 ,filter可以有參數(shù),該參數(shù)可以是任何 jQuery 選擇器的語(yǔ)法,
- children():獲取所有子元素,可以有參數(shù),該參數(shù)可以是任何 jQuery 選擇器的語(yǔ)法,
- parent():獲取父元素,可以有參數(shù),該參數(shù)可以是任何 jQuery 選擇器的語(yǔ)法,
- parents():獲取祖先元素,可以有參數(shù),該參數(shù)可以是任何 jQuery 選擇器的語(yǔ)法,
- siblings():獲取兄弟元素,可以有參數(shù),該參數(shù)可以是任何 jQuery 選擇器的語(yǔ)法,
from pyquery import PyQuery as pq
html="""
<html>
<head>
</head>
<body>
<h2>This is a heading</h2>
<p class="p1">This is a paragraph.</p>
<p class="p2">This is another paragraph.</p>
<div>
123
<a id="a1" rel="external nofollow" rel="external nofollow" rel="external nofollow" >hello</a>
</div>
<input type="Button" >
<input id="user" type="text" >
</body>
"""
###初始化
textParse = pq(html)
# urlParse = pq('http://www.baidu.com') #1
# urlParse = pq(url='http://www.baidu.com') #2
# fileParse = pq(filename="L:\demo.html")
##獲取
result = textParse('h2').text()
print(result)
result2= textParse('div').html()
print(result2)
result3=textParse(".p1").text()
print(result3)
result4=textParse("#user").attr("type")
print(result4)
result5=textParse("p,div").text()
print(result5)
result6=textParse("div a").attr.href
print(result6)
result7=textParse("[class='p1']").text()
print(result7)
result8=textParse("p:last").text()
print(result8)
result9=textParse("div").find("a").text()
print(result9)
result12=textParse("p").filter(".p1").text()
print(result12)
result10=textParse("div").children()
print(result10)
result11=textParse("a").parent()
print(result11)
6.元素的文本、屬性等內(nèi)容的獲取:
attr(attribute):獲取屬性
result2=textParse("a").attr("href")
attr.xxxx:獲取屬性xxxx
result21=textParse("a").attr.href
result22=textParse("a").attr.class_
text():獲取文本,子元素中也僅僅返回文本
result1=textParse("a").text()
html():獲取html,功能與text類似,但返回html標(biāo)簽
result3=textParse("div").html()
補(bǔ)充1:
元素的迭代:如果返回的結(jié)果是多個(gè)元素,如果想迭代出每個(gè)元素,可以使用items():

補(bǔ)充2:pyquery是jquery的python化,語(yǔ)法基本都是相通的,想了解更多,可以參考jquery。
pyquery執(zhí)行DOM操作、css操作:
DOM操作:
add_class():增加class
remove_class():移除class
remove():刪除指定元素
from pyquery import PyQuery as pq
html="""
<html>
<head>
</head>
<body>
<h2>This is a heading</h2>
<p id="p1" class="p1">This is a paragraph.</p>
<p class="p2">This is another paragraph.</p>
<div style="color:blue">
123
<a class="ca" rel="external nofollow" rel="external nofollow" rel="external nofollow" >hello</a>
</div>
<input type="Button" >
<input id="user" type="text" >
</body>
"""
textParse=pq(html)
textParse('a').add_class("c1")
print(textParse('a').attr("class"))
textParse('a').remove_class("c1")
print(textParse('a').attr("class"))
print(textParse('div').html())
textParse('div').remove("a")
print(textParse('div').html())
css操作:
- attr():設(shè)置屬性
- 設(shè)置格式:attr("屬性名","屬性值")
- css():設(shè)置css
- 設(shè)置格式1:css("css樣式","樣式值")
- 格式2:css({"樣式1":"樣式值","樣式2":"樣式值"})
from pyquery import PyQuery as pq
html="""
<html>
<head>
</head>
<body>
<h2>This is a heading</h2>
<p id="p1" class="p1">This is a paragraph.</p>
<p class="p2">This is another paragraph.</p>
<div style="color:blue">
123
<a class="ca" rel="external nofollow" rel="external nofollow" rel="external nofollow" >hello</a>
</div>
<input type="Button" >
<input id="user" type="text" >
</body>
"""
textParse=pq(html)
textParse('a').attr("name","hehe")
print(textParse('a').attr("name"))
textParse('a').css("color","white")
textParse('a').css({"background-color":"black","postion":"fixed"})
print(textParse('a').attr("style"))
這些操作什么時(shí)候會(huì)被用到:
【有時(shí)候可能會(huì)將數(shù)據(jù)樣式處理一下再存儲(chǔ)下來(lái),就需要用到,比如我獲取下來(lái)的數(shù)據(jù)樣式我不滿意,可以自定義成我自己的格式】
【有時(shí)候需要逐層清理再篩選出指定結(jié)果,比如<div>123<a></a></div>中,如果僅僅想要獲取123就可以先刪除<a>再獲取】
一個(gè)利用pyquery爬取豆瓣新書(shū)的例子:
先使用審查元素,定位目標(biāo)元素
確認(rèn)爬取信息
要注意的是,豆瓣新書(shū)是有一些分在后面頁(yè)的,實(shí)際上目標(biāo)應(yīng)該是li的上一級(jí)ul:
使用PyQuery篩選出結(jié)果:
from pyquery import PyQuery as pq
urlParse=pq(url="https://book.douban.com/")
info=urlParse("div.carousel ul li div.info")
file=open("demo.txt","w",encoding="utf8")
for i in info.items():
title=i.find("div.title")
author=i.find("span.author")
abstract=i.find(".abstract")
file.write("標(biāo)題:"+title.text()+"\n")
file.write("作者:"+author.text()+"\n")
file.write("概要:"+abstract.text()+"\n")
file.write("-----------------\n")
print("\n")
file.close()
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
在Python 2.7即將停止支持時(shí),我們?yōu)槟銕?lái)了一份python 3.x遷移指南
這篇文章主要介紹了在Python 2.7即將停止支持時(shí)我們?yōu)槟銣?zhǔn)備了一份python 3.x遷移指南的相關(guān)資料,需要的朋友可以參考下2018-01-01
Numpy?數(shù)據(jù)處理?ndarray使用詳解
這篇文章主要為大家介紹了Numpy?數(shù)據(jù)處理?ndarray使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
python Matplotlib底圖中鼠標(biāo)滑過(guò)顯示隱藏內(nèi)容的實(shí)例代碼
這篇文章主要介紹了python Matplotlib底圖中鼠標(biāo)滑過(guò)顯示隱藏內(nèi)容,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
使用python的pandas庫(kù)讀取csv文件保存至mysql數(shù)據(jù)庫(kù)
這篇文章主要介紹了利用python的pandas庫(kù)讀取csv文件保存至mysql數(shù)據(jù)庫(kù)的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
python中threading和queue庫(kù)實(shí)現(xiàn)多線程編程
這篇文章主要介紹了python中threading和queue庫(kù)實(shí)現(xiàn)多線程編程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
100行Python代碼實(shí)現(xiàn)自動(dòng)搶火車票(附源碼)
又到年底了,相信對(duì)于在外地的朋友們來(lái)說(shuō),火車票是到年底最頭痛的一件事了,但作為程序員的你怎么能一樣呢?快發(fā)揮你的特長(zhǎng),下面這篇文章主要給大家介紹了如果通過(guò)100行Python代碼實(shí)現(xiàn)自動(dòng)搶火車票的相關(guān)資料,需要的朋友可以參考下。2018-01-01
PyCharm創(chuàng)建Django項(xiàng)目的簡(jiǎn)單步驟記錄
PyCharm是一種Python?IDE,帶有一整套可以幫助用戶在使用Python語(yǔ)言開(kāi)發(fā)時(shí)提高其效率的工具,下面這篇文章主要給大家介紹了關(guān)于利用PyCharm創(chuàng)建Django項(xiàng)目的簡(jiǎn)單步驟,需要的朋友可以參考下2022-07-07

