Python中JsonPath提取器和正則提取器
一、前言
我們一般在做接口關(guān)聯(lián)時(shí),會(huì)通過(guò)保存中間變量實(shí)現(xiàn)接口關(guān)聯(lián),在關(guān)聯(lián)時(shí)就需要用到變量提取,那今天我們就介紹接口自動(dòng)化中變量提取的兩大神器:正則提取器和JsonPath提取器。
1.1 正則提取器
正則提?。ㄕ齽t表達(dá)式只能提取字符串的數(shù)據(jù))
1、re.seach:只匹配一個(gè)值,通過(guò)下標(biāo)[1]取值,沒有匹配到返回None
2、re.findall:匹配多個(gè)值,返回列表list,多個(gè)值通過(guò)下標(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
匹配通配符:
我們一般用(.*?)和(.+?)來(lái)匹配我們需要提取的數(shù)值
解釋:
- . 表示任意一個(gè)字符
- + 表示匹配它前面的表達(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 ,返回的是一個(gè)list,通過(guò)下標(biāo)取值,沒有返回None
JsonPath語(yǔ)法
| 符號(hào) | 描述 |
|---|---|
| $ | 查詢的根節(jié)點(diǎn)對(duì)象,用于表示一個(gè)json數(shù)據(jù),可以是數(shù)據(jù)或者對(duì)象 |
| @ | 過(guò)濾器,處理的當(dāng)前節(jié)點(diǎn)對(duì)象 |
| * | 獲取所有節(jié)點(diǎn) |
| . | 獲取子節(jié)點(diǎn) |
| . . | 遞歸搜索,篩選所有符合條件的節(jié)點(diǎn) |
| ?() | 過(guò)濾器表達(dá)式,篩選操作 |
| [a]或者[a,b] | 迭代器下標(biāo),表示一個(gè)或多個(gè)數(shù)組下標(biāo) |
1.4 JsonPath提取器具體使用
下面使用一個(gè)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字符串,可以通過(guò)一下代碼反序列化得到j(luò)son對(duì)象:
books=json.loads(bookJson)
2、查看store下的bicycle的color屬性
checkurl = "$.store.bicycle.color" print(jsonpath.jsonpath(data, checkurl)) # 輸出:['red']
3、輸出book節(jié)點(diǎn)中包含的所有對(duì)象
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)的第一個(gè)對(duì)象
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)中所有對(duì)象對(duì)應(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的所有對(duì)象
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)中所有價(jià)格小于10的對(duì)象
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的對(duì)象
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django之創(chuàng)建引擎索引報(bào)錯(cuò)及解決詳解
這篇文章主要介紹了Django之創(chuàng)建引擎索引報(bào)錯(cuò)及解決詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
python for循環(huán)remove同一個(gè)list過(guò)程解析
這篇文章主要介紹了python for循環(huán)remove同一個(gè)list過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
python實(shí)現(xiàn)飛機(jī)大戰(zhàn)微信小游戲
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)飛機(jī)大戰(zhàn)微信小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
python+logging+yaml實(shí)現(xiàn)日志分割
這篇文章主要為大家詳細(xì)介紹了python+logging+yaml實(shí)現(xiàn)日志分割,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
python pyg2plot的原理知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于python pyg2plot的原理知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以參考下。2021-02-02

