首先进入王者官方,找到“英雄资料”
摁下F12进入控制台,找到请求网址
复制到pyhton代码response = request.urlopen("https://pvp.qq.com/web201605/js/herolist.json")中
"""
3引号注释 用于文档 常出现在开头
爬虫流程:1.请求 2.解析 3.保存
1.找到请求地址(F12 调试工具 网络页签 刷新 搜索 查找)
"""
#从urllib 包下导入 request 请求工具
from urllib import request
#导入 数据转换模块
import json
#请求一个网址 将返回内容存入response响应对象
response = request.urlopen("https://pvp.qq.com/web201605/js/herolist.json")
#读取结果 解码utf8将最终内容放入response
response = response.read().decode("utf8")
# 将字符串response 转换为方便使用的对象
response = json.loads(response)
# 使用for遍历response
for data in response:
#解析中文名 和 头像地址
head_url = f'https://game.gtimg.cn/images/yxzj/img201606/heroimg/{data["ename"]}/{data["ename"]}.jpg'
cname = data["cname"]
#print(cname, head_url)
#向头像地址再次发起请求
head_response = request.urlopen(head_url)
head_response = head_response.read()
#将返回的头像数据保存在文件
file = open(f"../heads/{cname}.jpg", "wb")
file.write(head_response)
file.close()
print(f"保存{cname} 成功")
运行成功后,打开文件所在位置即可找到爬取的图片。