Scrape King of Glory Items with Python and Auto‑Generate Markdown & Excel
Learn how to use Python to crawl the official King of Glory website, retrieve hero equipment data in JSON, download item images via a thread pool, and automatically generate organized Markdown and Excel documentation, complete with code snippets and troubleshooting tips.
The author, a Python enthusiast, shares a step‑by‑step tutorial on using Python to scrape hero equipment information from the official King of Glory website and automate the creation of documentation.
1. Data Acquisition
The target site is the King of Glory official page. By navigating to the Heroes/Skins section and then to In‑game Items , the equipment data can be captured. The data is returned in JSON format, which can be inspected via browser network tools.
After locating the JSON endpoint ( https://pvp.qq.com/web201605/js/item.json), the data can be fetched directly.
Fetching Equipment Data
import requests
import pandas as pd
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36 '
}
target = 'https://pvp.qq.com/web201605/js/item.json'
item_list = requests.get(target, headers=headers).json()
item_df = pd.DataFrame(item_list)
item_df.sort_values(["item_type", "price", "item_id"], inplace=True)
item_df.fillna("", inplace=True)
item_df.des1 = item_df.des1.str.replace("</?p>", "", regex=True)
item_df.des2 = item_df.des2.str.replace("</?p>", "", regex=True)
item_dfThe resulting DataFrame contains fields such as item type, price, and descriptions.
Multithreaded Image Download
Using a thread pool, item images are downloaded quickly.
import os
from concurrent.futures import ThreadPoolExecutor
def download_img(item_id):
if os.path.exists(f"imgs/{item_id}.jpg"):
return
imgurl = f"http://game.gtimg.cn/images/yxzj/img201606/itemimg/{item_id}.jpg"
res = requests.get(imgurl)
with open(f"imgs/{item_id}.jpg", "wb") as f:
f.write(res.content)
os.makedirs("imgs", exist_ok=True)
with ThreadPoolExecutor(max_workers=8) as executor:
executor.map(download_img, item_df.item_id)Downloaded images are stored locally.
Generating Markdown Documentation
Data is pre‑processed and then written to a Markdown file, grouping items by type.
item_type_dict = {1: '攻击', 2: '法术', 3: '防御', 4: '移动', 5: '打野', 7: '游走'}
item_ids = item_df.item_id.values
item_df.item_id = item_df.item_id.apply(lambda item_id: f"")
item_df.item_type = item_df.item_type.map(item_type_dict)
item_df.columns = ["图片", "装备名称", "类型", "售价", "总价", "基础描述", "扩展描述"]
with open("王者装备说明.md", "w") as f:
for item_type, item_split in item_df.groupby("类型", sort=False):
f.write(f"# {item_type}
")
item_split.drop(columns="类型", inplace=True)
f.write(item_split.to_markdown(index=False))
f.write("
")The generated Markdown file contains tables for each equipment type.
Generating Excel Spreadsheet
For better layout control, the data is also exported to Excel with embedded images.
# Write to Excel
from openpyxl.drawing.image import Image
from openpyxl.styles import Alignment
with pd.ExcelWriter("王者装备说明.xlsx", engine='openpyxl') as writer:
item_df.to_excel(writer, sheet_name='装备说明', index=False)
worksheet = writer.sheets['装备说明']
worksheet.column_dimensions["A"].width = 11
for item_id, (cell,) in zip(item_ids, worksheet.iter_rows(2, None, 1, 1)):
worksheet.row_dimensions[cell.row].height = 67
worksheet.add_image(Image(f"imgs/{item_id}.jpg"), f'A{cell.row}')
worksheet.column_dimensions["F"].width = 15
worksheet.column_dimensions["G"].width = 35
writer.save()The resulting Excel file displays each item with its image, name, type, price, and descriptions.
2. Summary
This tutorial demonstrates how to fetch King of Glory hero equipment data using a Python web crawler, download item images with a thread pool, and automatically produce both Markdown and Excel documentation. The author invites readers to try the code, report issues, and join the Python learning community.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
