Python爬蟲JSON及JSONPath運(yùn)行原理詳解
JSON(JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式,它使得人們很容易的進(jìn)行閱讀和編寫。同時也方便了機(jī)器進(jìn)行解析和生成。適用于進(jìn)行數(shù)據(jù)交互的場景,比如網(wǎng)站前臺與后臺之間的數(shù)據(jù)交互。
JsonPath 是一種信息抽取類庫,是從JSON文檔中抽取指定信息的工具,提供多種語言實(shí)現(xiàn)版本,包括:Javascript, Python, PHP 和 Java。
JsonPath 對于 JSON 來說,相當(dāng)于 XPATH 對于 XML。
JsonPath與XPath語法對比:
Json結(jié)構(gòu)清晰,可讀性高,復(fù)雜度低,非常容易匹配,下表中對應(yīng)了XPath的用法。

相關(guān)推薦:《Python相關(guān)教程》
利用JSONPath爬取拉勾網(wǎng)上所有的城市
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib2
# json解析庫,對應(yīng)到lxml
import json
# json的解析語法,對應(yīng)到xpath
import jsonpath
url = "http://www.lagou.com/lbs/getAllCitySearchLabels.json"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'}
request = urllib2.Request(url, headers = headers)
response = urllib2.urlopen(request)
# 取出json文件里的內(nèi)容,返回的格式是字符串
html = response.read()
# 把json形式的字符串轉(zhuǎn)換成python形式的Unicode字符串
unicodestr = json.loads(html)
# Python形式的列表
city_list = jsonpath.jsonpath(unicodestr, "$..name")
#for item in city_list:
# print item
# dumps()默認(rèn)中文為ascii編碼格式,ensure_ascii默認(rèn)為Ture
# 禁用ascii編碼格式,返回的Unicode字符串,方便使用
array = json.dumps(city_list, ensure_ascii=False)
#json.dumps(city_list)
#array = json.dumps(city_list)
with open("lagoucity.json", "w") as f:
f.write(array.encode("utf-8"))
結(jié)果:

糗事百科爬取
利用XPATH的模糊查詢
獲取每個帖子里的內(nèi)容
保存到 json 文件內(nèi)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib2
import json
from lxml import etree
url = "http://www.qiushibaike.com/8hr/page/2/"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'}
request = urllib2.Request(url, headers = headers)
html = urllib2.urlopen(request).read()
# 響應(yīng)返回的是字符串,解析為HTML DOM模式 text = etree.HTML(html)
text = etree.HTML(html)
# 返回所有段子的結(jié)點(diǎn)位置,contains()模糊查詢方法,第一個參數(shù)是要匹配的標(biāo)簽,第二個參數(shù)是標(biāo)簽名部分內(nèi)容
node_list = text.xpath('//div[contains(@id, "qiushi_tag")]')
items ={}
for node in node_list:
# xpath返回的列表,這個列表就這一個參數(shù),用索引方式取出來,用戶名
username = node.xpath('./div/a/@title')[0]
# 取出標(biāo)簽下的內(nèi)容,段子內(nèi)容
content = node.xpath('.//div[@class="content"]/span')[0].text
# 取出標(biāo)簽里包含的內(nèi)容,點(diǎn)贊
zan = node.xpath('.//i')[0].text
# 評論
comments = node.xpath('.//i')[1].text
items = {
"username" : username,
"content" : content,
"zan" : zan,
"comments" : comments
}
with open("qiushi.json", "a") as f:
f.write(json.dumps(items, ensure_ascii=False).encode("utf-8") + "
")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python 實(shí)現(xiàn)Mac 屏幕截圖詳解
今天小編就為大家分享一篇對Python 實(shí)現(xiàn)Mac 屏幕截圖詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-10-10
詳解Python中__new__和__init__的區(qū)別與聯(lián)系
在Python中,每個對象都有兩個特殊的方法:__new__和__init__,本文將詳細(xì)介紹這兩個方法的不同之處以及它們之間的聯(lián)系,具有一定的參考價值,感興趣的可以了解一下2023-12-12
windows+vscode安裝paddleOCR運(yùn)行環(huán)境的步驟
這篇文章主要介紹了windows+vscode安裝paddleOCR運(yùn)行環(huán)境,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
python中cryptography庫的實(shí)現(xiàn)
本文主要介紹了python中cryptography庫的實(shí)現(xiàn),包括Fernet、hash、AES、RSA等加密算法的使用,具有一定的參加價值,感興趣的可以了解一下2025-01-01

