python解析庫詳解 PyQuery庫詳解

2021-10-12 11:39:03 字數 4014 閱讀 3976

通過這篇文章為大家介紹崔慶才老師對python爬蟲pyquery庫的講解,包括基本原理及其理論知識點

目錄:一、什麼是pyquery庫?

二、安裝

三、pyquery庫用法詳解

一、什麼是pyquery庫?

強大而靈活的網頁解析庫。如果你覺得正則寫起來太麻煩,如果你覺得beautifulsoup語法太難記,如果你熟悉jquery的語法,那麼pyquery就是你的絕佳選擇!!!

二、安裝

pip install pyquery

三、pyquery用法講解初始化(3種)

#字串初始化

html = '''

''' from pyquery import pyquery as pq

doc = pq(html)

print(doc('li'))#選擇器實際上就是css選擇器,即:選id就加「#」,選class前面加「.」

#url初始化

doc1 = pq(url = "")

print(doc1("head"))

#檔案初始化

print(doc2('li'))

基本css選擇器

#css選擇器

html = '''

''' doc3 = pq(html)

print(doc3("#container .list li"))#注意空格,空格代表巢狀關係

查詢元素

子元素#子元素(find)

html = '''

from pyquery import pyquery as pq

doc = pq(html)

items = doc(".list")#首先選中url標籤

print(type(items))

print(items)

lis = items.find('li')#實際上也是乙個css選擇器,將裡面所有的li標籤都列印出來;只要在它裡面的標籤都可以找到

print(type(lis))

print(lis)

#查詢直接子元素

lis2 = items.children()

print(type(lis2))

print(lis2)

lis3 = items.children('.active')

print(lis3)

父元素#父元素

html = '''

from pyquery import pyquery as pq

doc = pq(html)

items = doc(".list")#首先選中url標籤

#每個標籤外面肯定只能套乙個父元素

container = items.parent()

print(type(container))

print(container)

#父元素2

html = '''

'''from pyquery import pyquery as pq

doc = pq(html)

items = doc(".list")#首先選中url標籤

#將所有祖先節點返回

parents = items.parents()

print(parents)

print(type(parents))#列印出兩個div

#在其中進行搜尋

doc = pq(html)

items = doc(".list")

parents1 = items.parents(".wrap")

print(parents1)#通過篩選,只剩下乙個div

兄弟元素

#兄弟元素

html = '''

from pyquery import pyquery as pq

doc = pq(html)

li = doc('.list .item-0.active')#首先選class=「.list」,空格即使選擇list裡面的標籤,再選class=「item-0」,並列active(實際就是乙個整體)

print(li.siblings())#獲取所有的兄弟元素

#在向其中篩選

print(li.siblings('.active'))

遍歷#單個元素

html = '''

from pyquery import pyquery as pq

doc = pq(html)

li = doc(".item-0.active")

print(li)

lis = doc('li').items()#多個元素,進行遍歷,生成乙個產生器

print(type(lis))

for li in lis:

print(li)

獲取資訊

#獲取屬性

html = '''

from pyquery import pyquery as pq

doc = pq(html)

a = doc(".item-0.active a")#選擇class同時為item-0和active,在選擇class裡面的啊標籤,中間注意空格

print(a)

print(a.attr("href"))

print(a.attr.href)#結果同上

#獲取文字

print(a.text())#將上面的選中的class中包圍的文字

#獲取html

a1 = doc(".item-0.active")

print(a1.html())

dom操作

#addclass,removeclass

html = '''

from pyquery import pyquery as pq

doc = pq(html)

li = doc(".item-0.active")

print(li)

li.removeclass("active")#移除active

print(li)

li.addclass("active")#增加active

print(li)

#attr、css

doc = pq(html)

li = doc(".item-0.active")

li.attr("name","link")#若存在,就會覆蓋

print(li)

li.css("font-size","14px")#增加style屬性

print(li)

#remove

html1 = '''

hello,world

this is a paragraph.

from pyquery import pyquery as pq

doc = pq(html1)

wrap = doc(".wrap")

print(wrap.text())

wrap.find('p').remove()

print(wrap.text())

其他dom方法:pyquery: a jquery-like library for python​pythonhosted.org

#偽類選擇器

html = '''

from pyquery import pyquery as pq

doc = pq(html)

li = doc("li:first-child")#第乙個

print(li)

li1 = doc('li:last-child')#最後乙個

print(li1)

li2 = doc('li:nth-child(2)')#指定縮寫順序,第二個

print(li2)

li3 = doc("li:gt(2)")#大於2的

print(li3)

li4 = doc("li:nth-child(2n)")#偶數

print(li4)

li5 = doc("li:contains(second)")#內容包含second

print(li5)

python爬蟲之解析網頁的工具pyquery

主要是對這篇部落格所做的筆記 有疑惑可以去看這篇文章 from pyquery import pyquery as py 初始化的三種方式 doc py html doc py url encoding utf 8 doc py filename index.html 利用css選擇器 conten...

Python解析JSON詳解

json 函式 使用 json 函式需要匯入 json 庫 import json。函式 描述 json.dumps 將 python 物件編碼成 json 字串 json.loads 將已編碼的 json 字串解碼為 python 物件 json.dumps 語法 json.dumps obj,s...

Python解析JSON詳解

使用 json 函式需要匯入 json 庫 import json。json.dumps 將 python 物件編碼成 json 字串 json.loads 將已編碼的 json 字串解碼為 python 物件 語法json.dumps obj,skipkeys false,ensure ascii...