urllib_jsonpath的基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

import json
import urllib.request
url='https://dianying.taobao.com/cityAction.json?activityId&_ksTS=1721053680353_108&jsoncallback=jsonp109&action=cityAction&n_s=new&event_submit_doGetAllRegion=true'
headers={
#带冒号的请求头无效
'Accept':'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
'Accept-Language':'zh-CN,zh;q=0.9',
'Bx-V':'2.5.13',
'Cookie':'cna=oHFeHc4hjXoCAXyBKvZW8b+W; miid=997249091986201949; t=c9d2a6b731f7159365c03b078906a0ea; cookie2=1a04d2274cc3bf989cd229ae6d08ec7b; v=0; _tb_token_=eee63ef856535; isg=BBMTQKCjca9iIz37hu_ZiX6Sopc9yKeKLKSToMUwujJpRDPmTZk32jqSfjQqf_-C',
'Priority':'u=1, i',
'Referer':'https://dianying.taobao.com/',
'Sec-Ch-Ua':'"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
'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; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0',
'X-Requested-With':'XMLHttpRequest'
}
req=urllib.request.Request(url=url,headers=headers)
res=urllib.request.urlopen(req)
#print(res.read().decode('utf-8'))
html=res.read().decode('utf-8')
html=html.split('(')[1].split(')')[0]
with open('taobao.json','w',encoding='utf-8')as f:
f.write(html)

import jsonpath
obj=json.load(open('taobao.json','r',encoding='utf-8'))
city_list=jsonpath.jsonpath(obj,'$..regionName')
for i in city_list:
print(i)



urllib_jsonpath的基本使用
https://ianwusb.blog/2024/07/26/urllib_jsonpath的基本使用/
作者
Ianwusb
发布于
2024年7月26日
许可协议