json和jsonpath应用

作者: 博行天下 | 来源:发表于2017-11-13 09:36 被阅读305次
  • json简单说就是javascript中的对象和数组,所以这两种结构就是对象和数组两种结构,通过这两种结构可以表示各种复杂的结构
  • JsonPath 是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括:Javascript, Python, PHP 和 Java
  • JsonPath 对于 JSON 来说,相当于 XPATH 对于 XML
# -*- coding:utf-8 -*-

import json
import jsonpath
import urllib2

class Json():
    def __init__(self):
        self.url = "http://www.lagou.com/lbs/getAllCitySearchLabels.json"

    def get_json(self):
        headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:56.0)'}
        request = urllib2.Request(self.url,headers=headers)
        response = urllib2.urlopen(request)
        html = response.read()
        jsonobj = json.loads(html)
        # 获取城市名称
        namelist = jsonpath.jsonpath(jsonobj,'$..name')
        for name in namelist:
            print(name)

        # 把列表存储为字符串
        nametext = json.dumps(namelist,ensure_ascii=False)
        with open('name.txt','a') as file:
            file.write(nametext.encode("utf-8"))
            file.close


if __name__ == "__main__":
    jsono = Json()
    jsono.get_json()

相关文章

  • json和jsonpath应用

    json简单说就是javascript中的对象和数组,所以这两种结构就是对象和数组两种结构,通过这两种结构可以表示...

  • JSONPath入门及测试

    JSONPath入门 JSONPath - 是xpath在json的应用,是参照xpath表达式来解析XML文档的...

  • JSONPath 表达式的使用

    一、JSONPath使用需要的包 二、使用说明 1、JSONPath是xpath在json的应用2、JSONPat...

  • Python 使用 jsonpath 解析 json

    jsonpath 通过通用的表达式可以获取json中指定的值。Python中jsonpath的使用1、使用json...

  • JSONPath解析json

    JSONPath 用来解析多层嵌套的json数据,JsonPath 是一种信息抽取类库,是从JSON文档中抽取指定...

  • 1,JsonPath简介

    1,Jayway JsonPath Jayway JsonPath是一个读取json文档的java DSL 实现 ...

  • JSON Extractor

    Reference JsonPath What it is The JSON PostProcessor enab...

  • JsonPath解析Json数据

    JsonPath to JSON is what XPATH is to XML, a simple way to...

  • JsonPath基本使用

    JsonPath is to JSON what XPATH is to XML, a simple way to...

  • jsonpath 2022-01-16

    jsonpath是用于解析json文件的强大工具,不同语言都有各自的实现 jsonpath的设计思想和正则表达式相...

网友评论

    本文标题:json和jsonpath应用

    本文链接:https://www.haomeiwen.com/subject/wwmamxtx.html