Python中JsonPath提取器和正則提取器
一、前言
我們一般在做接口關(guān)聯(lián)時,會通過保存中間變量實(shí)現(xiàn)接口關(guān)聯(lián),在關(guān)聯(lián)時就需要用到變量提取,那今天我們就介紹接口自動化中變量提取的兩大神器:正則提取器和JsonPath提取器。
1.1 正則提取器
正則提?。ㄕ齽t表達(dá)式只能提取字符串的數(shù)據(jù))
1、re.seach:只匹配一個值,通過下標(biāo)[1]取值,沒有匹配到返回None
2、re.findall:匹配多個值,返回列表list,多個值通過下標(biāo)取值,沒有返回None
1.2 正則示例:
import re import requests a = requests.get("http://www.baidu.com") # print(a.text) b = re.search('charset=(.*?)><meta http-equiv=X-UA-Compatible content=IE=Edge>', a.text) print(b) print(b.group()) print(b.groups()) print(b.group(1))
結(jié)果:
<re.Match object; span=(94, 157), match='charset=utf-8><meta http-equiv=X-UA-Compatible co>
charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge>
('utf-8',)
utf-8
匹配通配符:
我們一般用(.*?)和(.+?)來匹配我們需要提取的數(shù)值
解釋:
- . 表示任意一個字符
- + 表示匹配它前面的表達(dá)式1次或者多次
- * 表示匹配它前面的表達(dá)式0次或者多次
- ? 表示匹配它前面的表達(dá)式1次或者多次
token = re.search('"token":"(.*?)",',res.text)[1] print("token1:%s",%token) token = re.findall('"token":"(.*?)",'res.text) print("token2:%s",%token)
1.3 JsonPath提取器
JsonPath提?。↗sonPath只能提取json格式的數(shù)據(jù))
jsonpath.jsonpath ,返回的是一個list,通過下標(biāo)取值,沒有返回None
JsonPath語法
符號 | 描述 |
---|---|
$ | 查詢的根節(jié)點(diǎn)對象,用于表示一個json數(shù)據(jù),可以是數(shù)據(jù)或者對象 |
@ | 過濾器,處理的當(dāng)前節(jié)點(diǎn)對象 |
* | 獲取所有節(jié)點(diǎn) |
. | 獲取子節(jié)點(diǎn) |
. . | 遞歸搜索,篩選所有符合條件的節(jié)點(diǎn) |
?() | 過濾器表達(dá)式,篩選操作 |
[a]或者[a,b] | 迭代器下標(biāo),表示一個或多個數(shù)組下標(biāo) |
1.4 JsonPath提取器具體使用
下面使用一個JSON文檔演示JSONPath的具體使用。JSON 文檔的內(nèi)容如下:
{ "store": { "book":[ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } } }
1、假設(shè)變量bookJson中已經(jīng)包含了這段json字符串,可以通過一下代碼反序列化得到j(luò)son對象:
books=json.loads(bookJson)
2、查看store下的bicycle的color屬性
checkurl = "$.store.bicycle.color" print(jsonpath.jsonpath(data, checkurl)) # 輸出:['red']
3、輸出book節(jié)點(diǎn)中包含的所有對象
checkurl = "$.store.book[*]" object_list = jsonpath.jsonpath(data, checkurl) print(object_list)
#輸出
[{'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95},
{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
4、輸出book節(jié)點(diǎn)的第一個對象
checkurl = "$.store.book[0]" obj = jsonpath.jsonpath(data, checkurl) print(obj) # 輸出: ['category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}]
5、輸出book節(jié)點(diǎn)中所有對象對應(yīng)的屬性title值
checkurl = "$.store.book[*].title" titles = jsonpath.jsonpath(data, checkurl) print(titles) # 輸出: ['Sayings of the Century', 'The Lord of the Rings']
6、輸出book節(jié)點(diǎn)中category為fiction的所有對象
checkurl = "$.store.book[?(@.category=='fiction')]" books=jsonpath.jsonpath(data, checkurl) print(books) #輸出 [{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
7、輸出book節(jié)點(diǎn)中所有價格小于10的對象
checkurl="$.store.book[?(@.price<10)]" books = jsonpath.jsonpath(data, checkurl) print(books) # 輸出: [{'category': 'reference', 'author': 'Nigel Rees', 'title':'Sayings of the Century', 'price': 8.95}]
8、輸出book節(jié)點(diǎn)中所有含有isb的對象
checkurl = "$.store.book[?(@.isbn)]" books = jsonpath.jsonpath(data,checkurl) print(books) # 輸出: [{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
到此這篇關(guān)于Python中JsonPath提取器和正則提取器的文章就介紹到這了,更多相關(guān)JsonPath提取器和正則提取器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python控制自己的手機(jī)攝像頭拍照并自動發(fā)送到郵箱案例講解
這篇文章主要介紹了Python控制自己的手機(jī)攝像頭拍照,并把照片自動發(fā)送到郵箱,大概思路是通過opencv調(diào)用攝像頭拍照保存圖像本地用email庫構(gòu)造郵件內(nèi)容,保存的圖像以附件形式插入郵件內(nèi)容用smtplib庫發(fā)送郵件到指定郵箱,需要的朋友可以參考下2022-04-04Python中使用pypdf2合并、分割、加密pdf文件的代碼詳解
這篇文章主要介紹了Python中使用pypdf2合并、分割、加密pdf文件的代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05詳解基于python的全局與局部序列比對的實(shí)現(xiàn)(DNA)
這篇文章主要介紹了詳解基于python的全局與局部序列比對的實(shí)現(xiàn)(DNA).文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10python如何生成隨機(jī)n位數(shù)字與字母組合(創(chuàng)建隨機(jī))
這篇文章主要介紹了python如何生成隨機(jī)n位數(shù)字與字母組合(創(chuàng)建隨機(jī)),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08Windows下實(shí)現(xiàn)將Pascal VOC轉(zhuǎn)化為TFRecords
今天小編就為大家分享一篇Windows下實(shí)現(xiàn)將Pascal VOC轉(zhuǎn)化為TFRecords,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02