import urllib.request url = 'https://dianying.taobao.com/cityAction.json?activityId&_ksTS=1629789477003_137&jsoncallback=jsonp138&action=cityAction&n_s=new&event_submit_doGetAllRegion=true' headers = { # ':authority': 'dianying.taobao.com', # ':method': 'GET', # ':path': '/cityAction.json?activityId&_ksTS=1629789477003_137&jsoncallback=jsonp138&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', 'cookie': 'cna=UkO6F8VULRwCAXTqq7dbS5A8; miid=949542021157939863; sgcookie=E100F01JK9XMmyoZRigjfmZKExNdRHQqPf4v9NIWIC1nnpnxyNgROLshAf0gz7lGnkKvwCnu1umyfirMSAWtubqc4g%3D%3D; tracknick=action_li; _cc_=UIHiLt3xSw%3D%3D; enc=dA18hg7jG1xapfVGPHoQCAkPQ4as1%2FEUqsG4M6AcAjHFFUM54HWpBv4AAm0MbQgqO%2BiZ5qkUeLIxljrHkOW%2BtQ%3D%3D; hng=CN%7Czh-CN%7CCNY%7C156; thw=cn; _m_h5_tk=3ca69de1b9ad7dce614840fcd015dcdb_1629776735568; _m_h5_tk_enc=ab56df54999d1d2cac2f82753ae29f82; t=874e6ce33295bf6b95cfcfaff0af0db6; xlly_s=1; cookie2=13acd8f4dafac4f7bd2177d6710d60fe; v=0; _tb_token_=e65ebbe536158; tfstk=cGhRB7mNpnxkDmUx7YpDAMNM2gTGZbWLxUZN9U4ulewe025didli6j5AFPI8MEC..; l=eBrgmF1cOsMXqSxaBO5aFurza77tzIRb8sPzaNbMiInca6OdtFt_rNCK2Ns9SdtjgtfFBetPVKlOcRCEF3apbgiMW_N-1NKDSxJ6-; isg=BBoas2yXLzHdGp3pCh7XVmpja8A8S54lyLj1RySTHq14l7vRDNufNAjpZ2MLRxa9', 'referer': 'https://dianying.taobao.com/', 'sec-ch-ua': '"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"', 'sec-ch-ua-mobile': '?0', '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/92.0.4515.159 Safari/537.36', 'x-requested-with': 'XMLHttpRequest', } request = urllib.request.Request(url = url, headers = headers) response = urllib.request.urlopen(request) content = response.read().decode('utf-8') # split 切割解决反扒 content = content.split('(')[1].split(')')[0] with open('074_尚硅谷_爬虫_解析_jsonpath解析淘票票.json','w',encoding='utf-8')as fp: fp.write(content) import json import jsonpath obj = json.load(open('074_尚硅谷_爬虫_解析_jsonpath解析淘票票.json','r',encoding='utf-8')) city_list = jsonpath.jsonpath(obj,'$..regionName') print(city_list)
进行python的爬虫学习。
jsonpath的初步使用。
-
首先,定义了请求的URL和HTTP headers。这些headers包括浏览器的User-Agent、Cookie等信息,目的是模拟浏览器发起请求以防止被服务器识别为爬虫并拒绝响应。
-
使用urllib.request.Request()创建一个Request对象,其中包含了目标URL及设置好的headers。
-
通过urllib.request.urlopen()发送请求并获取服务器返回的response对象。
-
读取response的内容,并使用UTF-8编码解码后,对内容进行切割处理,提取出JSON格式的有效数据部分。
-
将提取出的JSON数据写入到名为'074_尚硅谷_爬虫_解析_jsonpath解析淘票票.json'的文件中。
-
使用json.load()方法打开并加载上述文件中的JSON数据到一个Python字典对象(obj)。
-
引入jsonpath库,利用jsonpath.jsonpath()函数从obj中提取路径为'$..regionName'的数据,这个路径表达式表示搜索整个JSON结构中所有嵌套层级的"regionName"字段。
-
最后打印出所有的城市名(city_list)。
碳币兑换页面
<template> <div class="carbon-coin-page"> <el-row :gutter="20"> <!-- 碳币发放区域 --> <el-col :span="12"> <el-card class="box-card"> <div slot="header" class="clearfix"> <span>碳币发放</span> </div> <div> <p>系统发放碳币:</p> <el-input v-model="systemIssuedCoins" placeholder="请输入发放的碳币数量"></el-input> <el-button type="primary" @click="handleSystemIssue">确认发放</el-button> </div> </el-card> </el-col> <!-- 碳排放额度转换区域 --> <el-col :span="12"> <el-card class="box-card"> <div slot="header" class="clearfix"> <span>碳排放额度转换</span> </div> <div> <p>剩余碳排放额度:{{ remainingEmissionQuota }}</p> <el-form> <el-form-item label="输入要转换的额度"> <el-input v-model="convertibleQuota" placeholder="请输入转换额度"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="handleConvertToCoins">转换为碳币</el-button> </el-form-item> </el-form> <p>当前碳币余额:{{ carbonCoins }}</p> </div> </el-card> </el-col> </el-row> </div> </template> <script> export default { data() { return { systemIssuedCoins: 0, convertibleQuota: '', remainingEmissionQuota: 1000, // 假设初始剩余碳排放额度为1000单位 carbonCoins: 0, // 用户现有的碳币数量 }; }, methods: { handleSystemIssue() { // 处理系统发放碳币逻辑,这里仅做模拟示例 this.carbonCoins += Number(this.systemIssuedCoins); // 清空输入框 this.systemIssuedCoins = ''; }, handleConvertToCoins() { // 转换逻辑,根据规则将输入的碳排放额度转换为碳币 const convertedCoins = this.convertibleQuota * 1.2; this.carbonCoins += convertedCoins; this.remainingEmissionQuota -= this.convertibleQuota; // 清空输入框 this.convertibleQuota = ''; }, }, }; </script>
标签:12,指导,request,headers,json,jsonpath,sec,寒假,碳币 From: https://www.cnblogs.com/syhxx/p/17976916