xiaojipkhuang1 发表于 2024-9-3 09:25

请教大佬一个json数据整理问题

import requests
import hashlib
import json
import random
import time
from urllib.parse import quote
from curl_cffi import requests
import pymysql
from jsonpath_ng import jsonpath, parse
import re
from email.mime.text import MIMEText
from email.header import Header
import smtplib
import threading
from datetime import datetime
from tenacity import retry, stop_after_attempt, wait_fixed
import re


url = "https://api2.kkzhw.com/mall-search/kkSearch/search?productCategoryId=75&pageNum=1&pageSize=10"

json_data = {"sort": "desc", "order": "publishTime", "queryStrParams": [], "keyword": "", "attrValueList": [
    {"name": "账号专区", "ename": "", "selectType": 1, "inputType": 1, "sort": 60000, "filterType": 0, "searchType": 1,
   "type": 3, "searchSort": 0, "moreTxt": "展开更多", "value": "在售专区"},
    {"name": "职业", "selectType": 1, "inputType": 1, "sort": 40000, "filterType": 1, "searchType": 1, "type": 1,
   "searchSort": 0, "nameGroup": "官方截图", "moreTxt": "展开更多", "value": ""},
    {"name": "账号类型", "selectType": 1, "inputType": 1, "sort": 31000, "filterType": 0, "searchType": 1, "type": 1,
   "searchSort": 0, "nameGroup": "官方截图", "moreTxt": "展开更多", "value": ""},
    {"name": "已使用天赏石", "selectType": 0, "inputType": 0, "sort": 24000, "filterType": 0, "searchType": 2,
   "type": 1, "searchSort": 5, "nameGroup": "官方截图", "moreTxt": "展开更多", "value": "1-9999999"},
    {"name": "未使用天赏石", "selectType": 0, "inputType": 0, "sort": 23999, "filterType": 0, "searchType": 2,
   "type": 1, "searchSort": 8, "nameGroup": "", "moreTxt": "展开更多", "value": ""},
    {"name": "性别", "selectType": 1, "inputType": 1, "sort": 23000, "filterType": 0, "searchType": 1, "type": 1,
   "searchSort": 0, "nameGroup": "官方截图", "moreTxt": "展开更多", "value": ""},
    {"name": "评分", "selectType": 0, "inputType": 0, "sort": 22500, "filterType": 0, "searchType": 2, "type": 1,
   "searchSort": 3, "moreTxt": "展开更多", "value": ""},
    {"name": "衣品", "selectType": 0, "inputType": 0, "sort": 22000, "filterType": 0, "searchType": 2, "type": 1,
   "searchSort": 2, "moreTxt": "展开更多", "value": ""},
    {"name": "国色值", "selectType": 0, "inputType": 0, "sort": 9999, "filterType": 0, "searchType": 2, "type": 1,
   "searchSort": 1, "moreTxt": "展开更多", "value": ""},
    {"name": "换绑CD", "selectType": 1, "inputType": 1, "sort": 9999, "filterType": 0, "searchType": 1, "type": 1,
   "searchSort": 0, "nameGroup": "官方截图", "moreTxt": "展开更多", "value": ""},
    {"name": "转职CD", "selectType": 1, "inputType": 1, "sort": 9998, "filterType": 0, "searchType": 1, "type": 1,
   "searchSort": 0, "nameGroup": "官方截图", "moreTxt": "展开更多", "value": ""},
    {"name": "转性CD", "selectType": 1, "inputType": 1, "sort": 9997, "filterType": 0, "searchType": 1, "type": 1,
   "searchSort": 0, "nameGroup": "官方截图", "moreTxt": "展开更多", "value": ""},
    {"name": "稀有外观", "selectType": 2, "inputType": 1, "sort": 1000, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "moreTxt": "展开更多", "value": ""},
    {"name": "稀有道具", "selectType": 2, "inputType": 1, "sort": 900, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "nameGroup": "", "moreTxt": "展开更多", "value": ""},
    {"name": "灵韵数量", "selectType": 0, "inputType": 0, "sort": 101, "filterType": 0, "searchType": 2, "type": 1,
   "searchSort": 0, "moreTxt": "展开更多", "value": ""},
    {"name": "天赏发型", "selectType": 2, "inputType": 1, "sort": 99, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "moreTxt": "展开更多", "value": ""},
    {"name": "天赏祥瑞", "selectType": 2, "inputType": 1, "sort": 98, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "moreTxt": "展开更多", "value": ""},
    {"name": "天赏技能皮肤", "selectType": 2, "inputType": 1, "sort": 96, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "moreTxt": "展开更多", "value": ""},
    {"name": "其他天赏道具", "selectType": 2, "inputType": 1, "sort": 95, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "nameGroup": "", "moreTxt": "展开更多", "value": ""},
    {"name": "充值称号", "selectType": 2, "inputType": 1, "sort": 90, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "nameGroup": "", "moreTxt": "展开更多", "value": ""},
    {"name": "武器外观", "selectType": 2, "inputType": 1, "sort": 89, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "moreTxt": "展开更多", "value": ""},
    {"name": "轻功外观", "selectType": 2, "inputType": 1, "sort": 88, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "nameGroup": "", "moreTxt": "展开更多", "value": ""},
    {"name": "绝版外观", "selectType": 2, "inputType": 1, "sort": 87, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "nameGroup": "", "moreTxt": "展开更多", "value": ""},
    {"name": "灵韵内功", "selectType": 2, "inputType": 1, "sort": 4, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "nameGroup": "", "moreTxt": "展开更多", "value": ""},
    {"name": "极品内功", "selectType": 2, "inputType": 1, "sort": 3, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "moreTxt": "展开更多", "value": ""},
    {"name": "金色武器打造", "selectType": 2, "inputType": 1, "sort": 1, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "nameGroup": "", "moreTxt": "展开更多", "value": ""},
    {"name": "充值金额", "selectType": 0, "inputType": 0, "sort": 0, "filterType": 0, "searchType": 2, "type": 1,
   "searchSort": 0, "moreTxt": "展开更多", "value": ""},
    {"name": "金色项链打造", "selectType": 2, "inputType": 1, "sort": 0, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "nameGroup": "", "moreTxt": "展开更多", "value": ""},
    {"name": "金色鞋子打造", "selectType": 2, "inputType": 1, "sort": 0, "filterType": 1, "searchType": 3, "type": 2,
   "searchSort": 0, "nameGroup": "", "moreTxt": "展开更多", "value": ""},
    {"name": "金色衣服护腕打造", "selectType": 2, "inputType": 1, "sort": 0, "filterType": 1, "searchType": 3,
   "type": 2, "searchSort": 0, "nameGroup": "", "moreTxt": "展开更多", "value": ""}]}
headers = {
    'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
    'Content-Type': 'application/json',
    'Accept': '*/*',
    'Host': 'api2.kkzhw.com',
    'Connection': 'keep-alive'
    # 'Cookie': 'acw_tc=c8d1b415709a7a7273ee18f900302071e257443c1b145391dccd75990f63a79c'
}


def send_post_request(url, json_data, headers):
    response = requests.post(url, json=json_data, headers=headers)
    now = str(datetime.now())
    status_code = str(response.status_code)
    kankan = now + '   ' + status_code
    print(f"看看发送请求:{kankan}")
    try:
      data = json.loads(response.text)
    except json.JSONDecodeError as e:
      print(f"JSON解码错误: {e}")
    data1 = json.loads(json.dumps(response.text))
    data_list = data["data"]["list"]
    attributes_map = {}
    for lst in data_list:
      attr_value_list = lst["attrValueList"]
      for attr_value in attr_value_list:
            attributes_map] = attr_value['value']
      zh = attributes_map.get(108, '')
      tnr = attributes_map.get(166, '')
      dj = attributes_map.get(301, '')
      ly = attributes_map.get(336, '')
    jsonpath_expr = parse('$..list[*]')
    for match in jsonpath_expr.find(data):
      bh = str(match.value['productSn'])
      amount = round(int(match.value['price']))
      if amount is None:
            amount = 0
      gs = int(match.value['sortValue1'])
      if gs is None:
            gs = 0
      yp = int(match.value['sortValue2'])
      if yp is None:
            yp = 0
      info_time = str(match.value['publishTime'])
      tss1 = int(match.value['sortValue5'])
      tss2 = int(match.value['sortValue8'])
      tss = tss1 + tss2
      nss = str(match.value['description'])
      if nss is None:
            nss = str(match.value['subTitle'])
      dq1 = str(match.value['name'])
      dq2 = dq1.split('|')
      dq = dq2[:4]
      rate = round(amount / tss)
      product = {'编号': bh,
                     '大区': dq,
                     '内容': nss,
                     '天霓染':tnr,
                     '天赏石': tss,
                     '金额': amount,
                     '天赏石均价': rate,
                     '发布时间': info_time,
                     '国色': gs,
                     '衣品': yp
                     }
      print(product)

send_post_request(url, json_data, headers)




这段python可以直接运行,然后获取到的是我把json中list里的字段整理成一个product的字典,但是attrValueList的值我想根据指定的id获取并添加到每个编号对应的product中,不知道循环该怎么组合,求教

FlyingDragon 发表于 2024-9-3 10:02

没看出来你的数据是怎么对应的,需要attrValueList的数据整理下,以product中对应的属性为key,要取的数据为value,弄成一个字典,就可以取了

xiaojipkhuang1 发表于 2024-9-3 10:32

FlyingDragon 发表于 2024-9-3 10:02
没看出来你的数据是怎么对应的,需要attrValueList的数据整理下,以product中对应的属性为key,要取的数据 ...

我好像是分成了两个循环了,分别循环读取list中字段的值还有attrValueList的值,但是不知道怎么整合到一个字典里去

xmp788 发表于 2024-9-3 10:46

本帖最后由 xmp788 于 2024-9-3 10:47 编辑

xiaojipkhuang1 发表于 2024-9-3 10:32
我好像是分成了两个循环了,分别循环读取list中字段的值还有attrValueList的值,但是不知道怎么整合到一 ...
dict(zip(key,value))

xiaojipkhuang1 发表于 2024-9-3 10:54

xmp788 发表于 2024-9-3 10:46
dict(zip(key,value))

能具体写一下吗,大佬,我刚入门不太懂

xmp788 发表于 2024-9-3 11:08

xiaojipkhuang1 发表于 2024-9-3 10:54
能具体写一下吗,大佬,我刚入门不太懂

https://easychuan.cn/r/nqxgi?t=s

wxk0248 发表于 2024-9-3 11:22

可以补充下你期望获得的数据示例,说实在的我没看懂你想要的最终数据是什么样的,凭感觉可以用jsonpath处理

xiaojipkhuang1 发表于 2024-9-3 11:37

本帖最后由 xiaojipkhuang1 于 2024-9-3 11:40 编辑

就是这个代码结果打印出来是一个字典了,例如{'编号': 'NSH39258601', '大区': '紫禁之巅', '内容': '血河 【邮箱账号】29.6血河账号描述:大狐狸,pve秒伤32w,pvp也在玩的,大衣柜 充值金额:39385 天赏发型:喵语 千秋 璃光 琼海 天赏祥瑞:月川灵 流光溯 天赏技能皮肤:明河曙天·长鲸天斗 日曜八荒·轩辕剑 商城外观:鳞骨逢幽 天纵恒尊 斗战凌空 赤狐魅语 瀚雪冰封 贪狼摄星 衣香鬓影 玉泽凌烟 帝皇铠甲 白灵幻尘 玄证禅心 法度禅心 苍龙浮山 灵鹿苍风 鱼龙鳞潜 烽火浮生 金粉绮梦 舞乐火霓 玄冥道骨 武器轻功外观:伞梦 鹤舞云仙 揽华霄 烈火焚荒 ', '天霓染': '1', '天赏石': 17, '金额': 3800, '天赏石均价': 224, '发布时间': '2024-09-03T03:28:59.000+00:00', '国色': 35490, '衣品': 296300, '备注': ''}

这样一个字典格式,但是这个字典格式目前我不知道怎么把attr_value_list里面的值给加进去,我想要的就是一个这个字典里面,一个id对应的这些基本信息+attr_value_list里面的信息这样

xiaojipkhuang1 发表于 2024-9-3 11:38

xmp788 发表于 2024-9-3 11:08
https://easychuan.cn/r/nqxgi?t=s

好的,谢谢大佬,我学习下

xmp788 发表于 2024-9-3 13:41

xiaojipkhuang1 发表于 2024-9-3 11:38
好的,谢谢大佬,我学习下

去这里学习:https://www.bilibili.com/video/BV1c4411e77t?p=37&vd_source=69a31d5f8294c35f539b77fb421be6af
页: [1] 2
查看完整版本: 请教大佬一个json数据整理问题