Python爬虫使用jsonpath解析json数据

jsonpath只能爬取本地文件
xpath解析本地与服务器响应的文件.
import urllib.request
import urllib.parse

url = ''
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.8',
    'Cookie': '',
    'Referer': 'https://dianying.taobao.com/index.htm?n_s=new',
    'Sec-Ch-Ua': 'Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120',
    '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/120.0.0.0 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest'
}
requests = urllib.request.Request(url=url, headers=headers)
proxy = urllib.request.ProxyHandler()
opener = urllib.request.build_opener(proxy)
responses = opener.open(requests)
contends = responses.read().decode('utf-8')
str_s = contends.split('(')[1].split(')')[0]
with open("tpp.json", 'w', encoding='utf-8') as f:
    f.write(str_s)
print(str_s)

import jsonpath
import json


def js_decode():
    obj = json.load(open("tpp.json", "r", encoding="utf-8"))
    city_list = jsonpath.jsonpath(obj, "$..regionName")
    print(city_list)


if __name__ == '__main__':
    js_decode()

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值