本文主要是介绍jsonpath_解析例子代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
# _*_ coding : utf-8 _*_
# @Time : 2023-11-05 13:23
# @Author : haowen
# @File : jsonpath_解析_淘票票
# @Project : py练习
import urllib.request
url = 'https://dianying.taobao.com/cityAction.json?activityId&_ksTS=1699161894273_112&jsoncallback=jsonp113&action=cityAction&n_s=new&event_submit_doGetAllRegion=true'headers = {# ':authority': 'dianying.taobao.com',# ':method': 'GET',# ':path': '/cityAction.json?activityId&_ksTS=1699161894273_112&jsoncallback=jsonp113&action=cityAction&n_s=new&event_submit_doGetAllRegion=true',# ':scheme': 'https','accept': 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',# 'accept-encoding': 'gzip, deflate, br','accept-language': 'zh-CN,zh;q=0.9','bx-v': '2.5.3','cookie': 'thw=cn; cna=wmOPG9sJRAQCAbeG91OuBZOl; t=1ad48401ae8960ffd98c25aac7bc2103; cookie2=15c67ea814f626bf89d2b010b2c0c8f2; v=0; _tb_token_=eb5646bb0417e; xlly_s=1; tb_city=440100; tb_cityName="uePW3Q=="; tfstk=dthDnYO_9xyXC-EmdnFXgLqLFlZgGiN_UcCTX5EwUurS6qpjWCRiqDwN6KNYI5mz2jExkS7jFD3FMjpb61Vb15-pvBddlqNT_VpD9BdEo6HXvHdp9K7zsnxKHjGVEGK6sBNv_fL0Kkb67P7unwk0YqrqkqhPc-Bjuu2TIb4mrkWNKTWUMLC1wXTcCOw4PzqLv73j0; l=fBT7TJSVNAB73FHABO5Courza779UIp84sPzaNbMiIEGa6Gh9F9H-NCTZoIMWdtjgTCA3ety4B7GXRLHR3VMCc0c07kqm08-exvtaQtJe; isg=BGRk0BYq3ZDdNyiNqjldwjc1IGJW_Yhn0QmELH6EIi_yKQTzpgyS990H6YEx8cC_','referer': 'https://dianying.taobao.com/?spm=a1z21.3046609.city.4.32c0112a16qzqu&city=440100','sec-ch-ua': '";Not A Brand";v="99", "Chromium";v="94"','sec-ch-ua-mobile': '?0','sec-ch-ua-platform': '"Windows"','sec-fetch-dest': 'empty','sec-fetch-mode': 'cors','sec-fetch-site': 'same-origin','user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.188.400 QQBrowser/11.4.5226.400','x-requested-with': 'XMLHttpRequest',
}request = urllib.request.Request(url = url, headers = headers)
response = urllib.request.urlopen(request)content = response.read().decode('utf-8')content = content.split('(')[1].split(')')[0]
# print(content)
with open('jsonpath_解析_淘票票.json','w', encoding='utf-8') as fp:fp.write(content)import json
import jsonpathobj = json.load(open('jsonpath_解析_淘票票.json','r',encoding='utf-8'))city_list = jsonpath.jsonpath(obj,'$..regionName')
print(city_list)
结果:
这篇关于jsonpath_解析例子代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!