首先這個和英雄頁面是不一樣的,英雄頁面的圖片鏈接是直接放在源代碼里面的,直接就可以請求到,但是這個源代碼里面是沒有的
雖然在檢查頁面能夠搜索到,但是應該是動態加載的,源碼中搜不到該鏈接
然后就去看看是不是某個接口中返回的數據
刷新了一下返回了一個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]}...")