jsonpath简单解析淘票票网站,获取城市名称
代码如下:
import urllib.request
import json
import jsonpath
url = 'https://dianying.taobao.com/cityAction.json?activityId&_ksTS=1691330599914_108&jsoncallback=jsonp109&action=cityAction&n_s=new&event_submit_doGetAllRegion=true'
headers = {
'accept': '',
'accept-language': '',
'bx-v': '',
'cookie': '',
'referer': '',
'sec-ch-ua': '',
'sec-ch-ua-mobile': '',
'sec-ch-ua-platform': '',
'sec-fetch-dest': '',
'sec-fetch-mode': '',
'sec-fetch-site': '',
'user-agent': '',
'x-requested-with': ''
}
# 请求对象的定制
request = urllib.request.Request(url, headers=headers)
# 模拟浏览器发送请求
response = urllib.request.urlopen(request)
# 获取响应数据
content = response.read().decode('utf-8')
# split 切割
content = content.split('(')[1].split(')')[0]
# 保存本地
with open('files/taopiaopiao.json', 'w', encoding='utf-8') as fp:
fp.write(content)
# obj = json.loads(content)
obj = json.load(open('files/taopiaopiao.json', 'r', encoding='utf-8'))
# 获取地区名称
city_name = jsonpath.jsonpath(obj, '$..regionName')
print(city_name)