欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python爬蟲學(xué)習(xí)筆記之pyquery模塊基本用法詳解

 更新時(shí)間:2020年04月09日 11:13:39   作者:隨風(fēng)行云  
這篇文章主要介紹了python爬蟲學(xué)習(xí)筆記之pyquery模塊基本用法,結(jié)合實(shí)例形式詳細(xì)分析了python爬蟲pyquery模塊基本功能、用法及操作注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了python爬蟲學(xué)習(xí)筆記之pyquery模塊基本用法。分享給大家供大家參考,具體如下:

相關(guān)內(nèi)容:

  • pyquery的介紹
  • pyquery的使用
    • 安裝模塊
    • 導(dǎo)入模塊
    • 解析對象初始化
    • css選擇器
    • 在選定元素之后的元素再選取
    • 元素的文本、屬性等內(nèi)容的獲取
  • pyquery執(zhí)行DOM操作、css操作
    • Dom操作
    • CSS操作
  • 一個利用pyquery爬取豆瓣新書的例子

首發(fā)時(shí)間:2018-03-09 21:26


pyquery的介紹

  • pyquery允許對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.解析對象初始化:

【使用PyQuery初始化解析對象,PyQuery是一個類,直接將要解析的對象作為參數(shù)傳入即可

  • 解析對象為字符串時(shí)字符串初始化 :默認(rèn)情況下是字符串,如果字符串是一個帶http\https前綴的,將會認(rèn)為是一個url
    textParse = pq(html)
  • 解析對象為網(wǎng)頁時(shí)url初始化: 建議使用關(guān)鍵字參數(shù)url=
    # urlParse = pq('http://www.baidu.com') #1
    urlParse = pq(url='http://www.baidu.com') #2
  • 解析對象為文件時(shí)文件初始化:建議使用關(guān)鍵字參數(shù)filename=
    fileParse = pq(filename="L:\demo.html")
  • 解析完畢后,就可以使用相關(guān)函數(shù)或變量來進(jìn)行篩選,可以使用css等來篩選,

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 選擇器的語法,
  • filter():對結(jié)果進(jìn)行過濾,找出指定元素 ,filter可以有參數(shù),該參數(shù)可以是任何 jQuery 選擇器的語法,
  • children():獲取所有子元素,可以有參數(shù),該參數(shù)可以是任何 jQuery 選擇器的語法,
  • parent():獲取父元素,可以有參數(shù),該參數(shù)可以是任何 jQuery 選擇器的語法,
  • parents():獲取祖先元素,可以有參數(shù),該參數(shù)可以是任何 jQuery 選擇器的語法,
  • siblings():獲取兄弟元素,可以有參數(shù),該參數(shù)可以是任何 jQuery 選擇器的語法,
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)容的獲?。?/h2>

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)簽image

result3=textParse("div").html()

補(bǔ)充1:

元素的迭代:如果返回的結(jié)果是多個元素,如果想迭代出每個元素,可以使用items():

image

補(bǔ)充2:pyquery是jquery的python化,語法基本都是相通的,想了解更多,可以參考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í)候會被用到:

【有時(shí)候可能會將數(shù)據(jù)樣式處理一下再存儲下來,就需要用到,比如我獲取下來的數(shù)據(jù)樣式我不滿意,可以自定義成我自己的格式】

【有時(shí)候需要逐層清理再篩選出指定結(jié)果,比如<div>123<a></a></div>中,如果僅僅想要獲取123就可以先刪除<a>再獲取】


一個利用pyquery爬取豆瓣新書的例子:

先使用審查元素,定位目標(biāo)元素image

確認(rèn)爬取信息image

要注意的是,豆瓣新書是有一些分在后面頁的,實(shí)際上目標(biāo)應(yīng)該是li的上一級ul:image

使用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文件與目錄操作技巧匯總

希望本文所述對大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論