千家信息网

jsonpath怎么安装使用

发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,本篇内容主要讲解"jsonpath怎么安装使用",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"jsonpath怎么安装使用"吧!安装#!/usr/bin/e
千家信息网最后更新 2025年02月02日jsonpath怎么安装使用

本篇内容主要讲解"jsonpath怎么安装使用",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"jsonpath怎么安装使用"吧!

安装

#!/usr/bin/env python# -*- coding:utf-8 -*-import urllib2# json解析库,对应到lxmlimport json# json的解析语法,对应到xpathimport jsonpathurl = "http://www.lagou.com/lbs/getAllCitySearchLabels.json"headers = {    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}request = urllib2.Request(url, headers=headers)response = urllib2.urlopen(request)#  取出json文件里的内容,返回的格式是字符串html = response.read()# 把json形式的字符串转换成python形式的Unicode字符串unicodestr = json.loads(html)# Python形式的列表city_list = jsonpath.jsonpath(unicodestr, "$..name")# for item in city_list:#    print item# dumps()默认中文为ascii编码格式,ensure_ascii默认为Ture# 禁用ascii编码格式,返回的Unicode字符串,方便使用array = json.dumps(city_list, ensure_ascii=False)# json.dumps(city_list)# array = json.dumps(city_list)with open("city.json", "w") as f:    f.write(array.encode("utf-8"))

到此,相信大家对"jsonpath怎么安装使用"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0