我们可以将json字符串格式化,然后观察其数据结构,下面是代码部分
import jsonimport httpxwith httpx.Client() as client:headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'}url = "https://content-static.mihoyo.com/content/ysCn/getContentList?pageSize=20&pageNum=1&channelId=12"# 这里以20条公告为例data = http://www.kingceram.com/post/client.get(url=url, headers=headers)# 请求公告数据data = json.loads(data.text)# 将数据转换成字典data = data['data']['list']data_result = {}# 定义空字典,用作储存处理完毕的数据for i in range(0, 20):data_result.update({i: {"id": i + 1}})data_result[i]["title"] = data[i]["title"]try:# 防止链接为空data_result[i]["img"] = data[i]["ext"][1]["value"][0]["url"]# 获取图片链接except IndexError:data_result[i]["img"] = Nonedata_result[i]["url"] = f"https://ys.mihoyo.com/main/news/detail/{data[i]['contentId']}"print(data_result)
运行程序我们得到下列结果
{0: {'id': 1, 'title': '《原神》3.5版本「风花的呼吸」更新说明', 'img': 'https://webstatic.mihoyo.com/upload/contentweb/2023/02/28/c9ce9c6451451fa554ab74c2342d2c19_7924384204652977964.png', 'url': 'https://ys.mihoyo.com/main/news/detail/26400'}, 1: {'id': 2, 'title': '《原神》「风花的呼吸」3.5版本更新维护预告', 'img': 'https://webstatic.mihoyo.com/upload/contentweb/2023/02/27/48fe0090e4376a66b2f52007f8dc8e77_4916568639223877411.jpg', 'url': 'https://ys.mihoyo.com/main/news/detail/26370'}, 2: {'id': 3, 'title': '《原神》预下载已开启', 'img': 'https://webstatic.mihoyo.com/upload/contentweb/2023/01/16/adaca686a9d402cab8d822fa10732988_6944874481639867125.jpg', 'url': 'https://ys.mihoyo.com/main/news/detail/25734'}, 3: {'id': 4, 'title': '《原神》3.4版本「磬弦奏华夜」更新维护预告', 'img': 'https://webstatic.mihoyo.com/upload/contentweb/2023/01/16/7f341ba2332a05fba996d820334bdebb_2996032226775512164.jpg', 'url': 'https://ys.mihoyo.com/main/news/detail/25733'}, 4: {'id': 5, 'title': '《原神》3.3版本「六入尽明,诸相皆无」更新说明', 'img': 'https://webstatic.mihoyo.com/upload/contentweb/2022/12/07/5cc3065b6ef051d74479baf232831e35_7777248077405843881.jpg', 'url': 'https://ys.mihoyo.com/main/news/detail/25037'}, 5: {'id': 6, 'title': '《原神》3.3版本更新维护预告', 'img': 'https://webstatic.mihoyo.com/upload/contentweb/2022/12/04/48fe0090e4376a66b2f52007f8dc8e77_3458819542487902745.jpg', 'url': 'https://ys.mihoyo.com/main/news/detail/24955'}, 6: {'id': 7, 'title': '《原神》「虚空鼓动,劫火高扬」3.2版本更新维护预告', 'img': 'https://webstatic.mihoyo.com/upload/contentweb/2022/10/31/48fe0090e4376a66b2f52007f8dc8e77_2523267794398635617.jpg', 'url': 'https://ys.mihoyo.com/main/news/detail/24436'}, 7: {'id': 8, 'title': '《原神》3.1版本「赤土之王与三朝圣者」更新说明', 'img': 'https://webstatic.mihoyo.com/upload/contentweb/2022/09/27/3efa40b0e761d8f15fc640b7e12bfbf6_5749587768232257187.jpg', 'url': 'https://ys.mihoyo.com/main/news/detail/22749'}, 8: {'id': 9, 'title': '《原神》3.1版本「赤土之王与三朝圣者」更新维护预告', 'img': 'https://webstatic.mihoyo.com/upload/contentweb/2022/09/26/7f341ba2332a05fba996d820334bdebb_734662277370844096.jpg', 'url': 'https://ys.mihoyo.com/main/news/detail/22714'}, 9: {'id': 10, 'title': '《原神》「千朵玫瑰带来的黎明」3.0版本更新维护预告', 'img': 'https://webstatic.mihoyo.com/upload/contentweb/2022/08/22/a926989766b3a74ae0a24917c7843b4d_6141685495939164846.jpg', 'url': 'https://ys.mihoyo.com/main/news/detail/22284'}, 10: {'id': 11, 'title': '《原神》2.5版本「薄樱初绽时」更新通知', 'img': None, 'url': 'https://ys.mihoyo.com/main/news/detail/20165'}, 11: {'id': 12, 'title': '《原神》2.5版本「薄樱初绽时」更新通知', 'img': None, 'url': 'https://ys.mihoyo.com/main/news/detail/20020'}, 12: {'id': 13, 'title': '「流光飞彩」海灯节答谢邮件发放问题处理说明', 'img': None, 'url': 'https://ys.mihoyo.com/main/news/detail/19990'}, 13: {'id': 14, 'title': '《原神》2.4版本「飞彩镌流年」更新说明', 'img': None, 'url': 'https://ys.mihoyo.com/main/news/detail/19560'}, 14: {'id': 15, 'title': '《原神》「飞彩镌流年」2.4版本更新通知', 'img': None, 'url': 'https://ys.mihoyo.com/main/news/detail/19494'}, 15: {'id': 16, 'title': '《原神》预下载即将开启', 'img': None, 'url': 'https://ys.mihoyo.com/main/news/detail/19427'}, 16: {'id': 17, 'title': '《原神》「皑尘与雪影」2.3版本更新说明', 'img': 'https://uploadstatic.mihoyo.com/contentweb/20211123/2021112319360636499.jpg', 'url': 'https://ys.mihoyo.com/main/news/detail/18865'}, 17: {'id': 18, 'title': '《原神》「皑尘与雪影」2.3版本更新通知', 'img': 'https://uploadstatic.mihoyo.com/contentweb/20211122/2021112210502220085.jpg', 'url': 'https://ys.mihoyo.com/main/news/detail/18664'}, 18: {'id': 19, 'title': '《原神》「雾海悬谜境」2.2版本活动说明', 'img': None, 'url': 'https://ys.mihoyo.com/main/news/detail/18075'}, 19: {'id': 20, 'title': '《原神》「雾海悬谜境」2.2版本更新通知', 'img': None, 'url': 'https://ys.mihoyo.com/main/news/detail/18069'}}
- chatgpt赋能python:Python怎么做选择题?
- chatgpt赋能python:PythonU盘:如何使用Python为U盘增加
- chatgpt赋能python:Python与宏的区别
- chatgpt赋能python:Python数据散点图:用于数据可视化的强大工具
- chatgpt赋能python:如何用Python获取数据
- chatgpt赋能python:如何使用Python生成模拟数据
- 关键词提取原则,如何提取关键词
- chatgpt赋能python:Python怎么来的
- chatgpt赋能python:基于Python的聊天系统:打造高效便利的沟通利
- chatgpt赋能python:Python自带方法:汉字转为拼音(Pinyin