目录
一、明文和密文的转换
1、什么是明文密文?
2、明文和密文之间的转换
代码演示如下
二、json模块的使用
1、什么是json?
2、json字符串和字典的转换
代码演示:
三、jsonpath模块的使用
1、jsonpath的介绍
2、jsonpath和xpath语法对比
3、代码演示获取节点信息
示例:获取Json数据并写到Json文件中
一、明文和密文的转换
1、什么是明文密文?
- 例如浏览器搜索,将查找的网址复制下来
- 然后粘贴到pycharm,会看到搜索的文字被加密了
2、明文和密文之间的转换
导入的模块:from urllib.parse import quote, unquote
- quote():将明文转为密文,将字符串编码转换成%xx%xx的形式
- unquote():将密文转为明文
代码演示如下
二、json模块的使用
1、什么是json?
json本质是字符串
2、json字符串和字典的转换
json.loads():把字符串转换成字典(将json数据转换成python数据)
json.dumps():把字典转换成字符串(将python数据转换成json数据)
代码演示:
三、jsonpath模块的使用
1、jsonpath的介绍
用来解析多层检讨的json数据,jsonpath是一种信息抽取类的库,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括:Javascript, Python, PHP 和 Java。
- 安装方法:pip install jsonpath
- 导入的模块:from jsonpath import jsonpath
- 使用方法:jsonpath(a, 'jsonpath语法规则字符串')
2、jsonpath和xpath语法对比
JSONPath | 描述 |
$ | 根节点 |
@ | 现行节点 |
. or [] | 取子节点 |
n/a | 取父节点,Jsonpath未支持 |
.. | 就是不管位置,选择所有符合条件的条件 |
* | 匹配所有元素节点 |
n/a | 根据属性访问,Json不支持,因为Json是个Key-value递归结构,不需要属性访问 |
[] | 迭代器标示(可以在里边做简单的迭代操作,如数组下标,根据内容选值等) |
[ , ] | 支持迭代器中做多选 |
?() | 支持过滤操作 |
() | 支持表达式计算 |
n/a | 分组,JsonPath不支持 |
3、代码演示获取节点信息
from jsonpath import jsonpath
json_data = { "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "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
}
}
}
authors = jsonpath(json_data, '$.store.book[*].author')
print(authors)
JSONPath | Result |
$.store.book[*].author | store中的所有book的作者 |
$..author | 所有的作者 |
$.store.* | store下的所有元素 |
$.store..price | store中的所有的内容的价格 |
$..book[2] | 第三本书 |
$..[(@.length-1)] | $..book[-1:] | 最后一本书 |
$..book[0,1] | $..book[2] | 前两本书 |
$..book[?(@.isbn)] | 获取有isbn的所有数 |
$..book[?(@.price<10)] | 获取价格大于10的所有的书 |
$..* | 获取所有的数据 |
示例:获取Json数据并写到Json文件中
import requests
from jsonpath import jsonpath
import json
url = 'http://www.lagou.com/lbs/getAllCitySearchLabels.json'
headers = {
"User-Agent": '', # 用自己的用户代理
'Cookie': 'user_trace_token=20220302110548-338d098e-48b4-4074-a7b2-b0f95b17dae6; _ga=GA1.2.703880885.1646190350; LGUID=20220302110549-3e4d19be-2e23-47ce-806e-5472c387a614; gate_login_token=3aaf853c2a4614fb225e1cfa5ae5aabf0e40c5d95450de28407db6e9ee0338c7; LG_HAS_LOGIN=1; hasDeliver=0; privacyPolicyPopup=false; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2217f4898527bd44-0cfe89f24613da-576153e-1327104-17f4898527cf27%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24os%22%3A%22Windows%22%2C%22%24browser%22%3A%22Chrome%22%2C%22%24browser_version%22%3A%2298.0.4758.82%22%7D%2C%22%24device_id%22%3A%2217f4898527bd44-0cfe89f24613da-576153e-1327104-17f4898527cf27%22%7D; JSESSIONID=ABAAAECABIEACCA9E3705A7B26198DCA05569D9A87A6FB4; X_HTTP_TOKEN=530d3411b134c39f92061364611eb3d44ebbbf6d46; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1646190349,1646316030; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1646316030; _gat=1; LGSID=20220303220030-b12e3c3a-168b-4215-84ed-cbaf1fafacfe; PRE_UTM=m_cf_cpt_baidu_pcbt; PRE_HOST=www.baidu.com; PRE_SITE=https%3A%2F%2Fwww.baidu.com%2Fother.php%3Fsc.0f0000jkQbdl5or9r9UPgjsEmfBHB8OhCeGOYbFNJM7h-xooDC%5FbOJmotBqOI2AOFo6DSqo%5F31tmAol8eDvpZJgp-S6IfjMALevAgY2bnlL63szi4ncryzw%5F-fr9OU6t3LsuDddkn2U%5FDBy77IxSw5sNaRegTT8G-Gm4zNh1Ba44GIVRS0zuU39cQJJQETAxDnishZ4F%5FO49TfbzD4nHlzwwq9k6.7Y%5FNR2Ar5Od663rj6tJQrGvKD77h24SU5WudF6ksswGuh9J4qt7jHzk8sHfGmYt%5FrE-9kYryqM764TTPqKi%5FnYQZHuukL0.TLFWgv-b5HDkrfK1ThPGujYknHb0THY0IAYqs2v4VnL30ZN1ugFxIZ-suHYs0A7bgLw4TARqnsKLULFb5TaV8UHPS0KzmLmqnfKdThkxpyfqnHR1n1mYPjc3r0KVINqGujYkPjmzPWnknfKVgv-b5HDkn1c1nj6d0AdYTAkxpyfqnHczP1n0TZuxpyfqn0KGuAnqiD4K0ZKGujYkn6KWpyfqn0KWThnqPjfvrHT%26dt%3D1646316021%26wd%3D%25E6%258B%2589%25E5%258B%25BE%25E7%25BD%2591%26tpl%3Dtpl%5F12273%5F25897%5F22126%26l%3D1533644288; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2Flanding-page%2Fpc%2Fsearch.html%3Futm%5Fsource%3Dm%5Fcf%5Fcpt%5Fbaidu%5Fpcbt; LGRID=20220303220030-77e25e7e-ec71-4d1e-bc4a-7b9b005a924c'
}
response =requests.get(url, headers=headers)
html_str = response.content.decode()
# 把json格式字符串转换成python对象
jsonobj = json.loads(html_str)
# 从根节点开始,匹配name节点
citylist = jsonpath(jsonobj,'$..name')
fp = open('city.json','wb')
content = json.dumps(citylist, ensure_ascii=False)
fp.write(content.encode('utf-8'))
fp.close()