1.jsonpath
2.通過jsonpath實戰
一.Jasonpath核心符號
1)$:
-
含義:表示 JSON 文檔的根節點。
-
用法:所有 JSONPath 表達式都以?
$
?開頭,表示從根節點開始查詢。
{"store": {"book": [{"title": "Book 1", "price": 10},{"title": "Book 2", "price": 20}]}
}
$
?表示整個 JSON 文檔。
2) . :
-
含義:用于訪問對象的屬性。
-
用法:通過點號?
.
?可以訪問對象的屬性。
$.store
?表示獲取?store
?對象。
$.store.book
?表示獲取?store
?對象中的?book
?數組。
from jsonpath import jsonpath
data={"store": {"book": [{"title": "Book 1", "price": 10},{"title": "Book 2", "price": 20}]}
}
store=jsonpath(data,"$.store")
book = jsonpath(data,"$.store.book")
print(store)
print(book)
[{'book': [{'title': 'Book 1', 'price': 10}, {'title': 'Book 2', 'price': 20}]}]
[[{'title': 'Book 1', 'price': 10}, {'title': 'Book 2', 'price': 20}]]
3) [ ]:
-
含義:用于訪問數組元素或對象的屬性(支持屬性名或索引)。
-
用法:
-
訪問數組元素:
[index]
-
訪問對象屬性:
['property']
-
$.store.book[0]
?表示獲取?book
?數組的第一個元素。
$['store']['book'][0]
?也可以用于訪問屬性。
from jsonpath import jsonpath
data={"store": {"book": [{"title": "Book 1", "price": 10},{"title": "Book 2", "price": 20}]}
}
a = jsonpath(data,"$.store[book]")
b = jsonpath(data,"$[store][book][0]")
print(a)
print(b)
[[{'title': 'Book 1', 'price': 10}, {'title': 'Book 2', 'price': 20}]]
[{'title': 'Book 1', 'price': 10}]
4) .. :
-
含義:遞歸下降,表示從當前節點開始遞歸搜索所有子節點。
-
用法:用于查找 JSON 文檔中所有符合條件的節點。
$..title
?表示在整個 JSON 文檔中查找所有?title
?屬性。
$..book
?表示查找所有?book
?節點。
from jsonpath import jsonpath
data={"store": {"book": [{"title": "Book 1", "price": 10},{"title": "Book 2", "price": 20}]}
}
a = jsonpath(data,"$..title")
b = jsonpath(data,"$..book")
print(a)
print(b)
['Book 1', 'Book 2']
[[{'title': 'Book 1', 'price': 10}, {'title': 'Book 2', 'price': 20}]]
5)@:
-
含義:在過濾器表達式中表示當前節點。
-
用法:用于在?
?()
?中引用當前節點。
$.store.book[?(@.price < 10)]
?中的?@
?表示當前?book
?元素。
6)?():
-
含義:過濾器表達式,用于根據條件篩選數據。
-
用法:在?
?()
?中編寫條件表達式,篩選符合條件的節點。
$.store.book[?(@.price < 10)]
?表示獲取?book
?數組中價格小于 10 的所有元素。
$.store.book[?(@.category == 'fiction')]
?表示獲取?category
?為?fiction
?的書籍。
二.實戰
當網頁返回的源代碼為json格式時,使用JSONPATH進行網頁爬取
1.通過判斷,騰訊網?為異步加載,所以要查看Fetch/XHR
2.在通過jsonpath獲取想要的信息部分
import requests
from jsonpath import jsonpath
import random
def get_data(url,headers,payload):response = requests.post(url, headers=headers,json=payload)if response.status_code == 200:html_data = response.json()return html_dataelse:print("請求代碼:",response.status_code)def parse_json(html_data):titles = jsonpath(html_data,'$.data..title')times = jsonpath(html_data,'$..publish_time')hrefs = jsonpath(html_data,'$..link_info[url]')for title,time,href in zip(titles,times,hrefs):print("新聞標題:",title)print("發布時間:",time)print("新聞鏈接:",href)if __name__ == '__main__':url="https://i.news.qq.com/web_feed/getHotModuleList"USER_AGENTS = ["Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)","Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)","Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)","Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)","Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)"]headers = {"user-agent": random.choice(USER_AGENTS),"cookie": "你的cookies"}payload={"你的瀏覽器requests payload"}html_data = get_data(url,headers,payload)parse_json(html_data)