|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Stubborn 于 2019-4-14 04:06 编辑
js动态加载的数据都是给你返回的json串数据,和xpath差不多的吧。
- # -*- coding: utf-8 -*-
- # @Time : 2019-04-08 00:35
- # @Author : Ein
- # @File : xpath和jsonxpath对比.py
- # @Software: PyCharm
- book_json = {"store": {
- "book": [
- {
- "category": "数学",
- "author": "Ein",
- "title": "初中高数",
- "price": 8.95
- },
- {
- "category": "历史",
- "author": "Ethan",
- "title": "中庸",
- "isbn":"123456",
- "price": 16
- },
- {
- "category": "化学",
- "author": "Eining",
- "title": "氮气",
- "isbn":"8722058",
- "price": 17.85
- },
- {
- "category": "物理",
- "author": "Stubbron",
- "title": "牛顿第三定律",
- "price": 88.88
- },
- {
- "category": "英语",
- "author": "loae",
- "title": "六级英语大纲",
- "price": 77.68
- },
- {
- "category": "政治",
- "author": "习大大",
- "title": "三进三出",
- "price": 27.7
- }
- ],
- "bicycle": {
- "color": "red",
- "pricr": 19.95
- }
- }}
- import jsonpath
- #jsonxpath和xpath
- #查找书点所有的author
- ret = jsonpath.jsonpath(book_json,'$.store.book[*].author')
- #res = tree.xpath('/store/boob/author')
- #所有的作者
- ret1 = jsonpath.jsonpath(book_json,'$..author')
- #res1 = tree.xpath('//author')
- #查找store下所有的节点
- ret2 = jsonpath.jsonpath(book_json,'$.store.*')
- #res2 = tree.xpath('/store/*')
- # 查找store下所有的price
- ret3 = jsonpath.jsonpath(book_json,'$.store..price')
- #res2 = tree.xpath('//store//price')
- #查找第三个book,返回的是一个列表
- ret4 = jsonpath.jsonpath(book_json,'$..book[2]')
- #res2 = tree.xpath('//book[3]')
- #查找最后一本book
- ret5 = jsonpath.jsonpath(book_json,'$..book[(@.length-1)]')
- #res2 = tree.xpath('//book[last()]')
- #过滤包含有isbn键值的数据
- ret6 = jsonpath.jsonpath(book_json,'$..book[?(@.isbn)]')
- #res2 = tree.xpath('//book[isbn]')
- #过滤price低于20的书籍
- ret7 = jsonpath.jsonpath(book_json,'$..book[?(@.price<10)]')
- #res2 = tree.xpath('//book[price<10]')
复制代码 |
|