Python中JsonPath提取器和正則提取器
一、前言
我們一般在做接口關(guān)聯(lián)時,會通過保存中間變量實現(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é)點對象,用于表示一個json數(shù)據(jù),可以是數(shù)據(jù)或者對象 |
| @ | 過濾器,處理的當(dāng)前節(jié)點對象 |
| * | 獲取所有節(jié)點 |
| . | 獲取子節(jié)點 |
| . . | 遞歸搜索,篩選所有符合條件的節(jié)點 |
| ?() | 過濾器表達(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é)點中包含的所有對象
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é)點的第一個對象
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é)點中所有對象對應(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é)點中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é)點中所有價格小于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é)點中所有含有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 for循環(huán)remove同一個list過程解析
這篇文章主要介紹了python for循環(huán)remove同一個list過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08
python+logging+yaml實現(xiàn)日志分割
這篇文章主要為大家詳細(xì)介紹了python+logging+yaml實現(xiàn)日志分割,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07

