王者荣耀道具页面爬虫(json格式数据)
首先这个和英雄页面是不一样的,英雄页面的图片链接是直接放在源代码里面的,直接就可以请求到,但是这个源代码里面是没有的
虽然在检查页面能够搜索到,但是应该是动态加载的,源码中搜不到该链接
然后就去看看是不是某个接口中返回的数据
刷新了一下返回了一个json
估计一些数据在这里面,我们下载下来试试
没错,那接下来就是简单的拼接了
下面是实现code
import requests
import csv
from urllib.request import urlretrieve
import json
# 1. 获取JSON数据
url = "https://pvp.qq.com/web201605/js/item.json"
try:
response = requests.get(url)
response.raise_for_status() # 检查HTTP错误
data = response.json()
except Exception as e:
print(f"获取数据失败: {e}")
exit()
# 2. 提取item_id和item_name
items = []
for item in data:
try:
items.append({
"item_id": item["item_id"],
"item_name": item["item_name"]
})
except KeyError:
print(f"跳过无效数据项: {item}")
continue
#载图片
print(items)
for item in items:
try:
img_url = f"https://game.gtimg.cn/images/yxzj/img201606/itemimgo/{item['item_id']}.png"
urlretrieve(img_url, f"D:/小说/王者荣耀武器道具/{item['item_name']}.png")
print(f"下载成功: {item['item_name']}.png")
except Exception as e:
print(f"下载失败 {item['item_name']}.png: {str(e)[:50]}...")