- 百度英雄联盟,进入官网,在游戏资料里面找到所有英雄数据
- 在页面F12中network中找到hero_list.js,所有的数据都在这里面,接下来就简单了,直接放上代码
将数据储存到mysql
import requests
import json
import pymysql
url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
conn = pymysql.Connect(host='127.0.0.1', port=3306, db='xtj_1', password='xtj', user='root', charset='utf8')
cursor = conn.cursor()
resp = requests.get(url)
hero_list = json.loads(resp.text)
for i in hero_list['hero']:
# print(i['name'],i['title'])
x = 'https://game.gtimg.cn/images/lol/act/img/champion/' + i['alias'] + '.png'
cursor.execute("insert into hero1(name,title,href) values(%s,%s,%s)", (i['name'], i['title'],x))
conn.commit()
从mysql中拿出数据,在桌面上新建文件夹并保存图片
import pymysql
import requests
import os
fpath ='e:/desketop/hero/'
conn = pymysql.Connect(host='127.0.0.1',port=3306,user='root',password='xtj',charset='utf8',db='xtj_1')
cursor=conn.cursor()
cursor.execute("select href from hero1")
result =cursor.fetchall()
for i in result:
# print(i[0])
resp=requests.get(i[0])
if not os.path.exists(fpath):
os.mkdir(fpath)
print('创建文件夹')
if not os.path.exists(fpath + i[0].split('/')[-1]):
with open(fpath + i[0].split('/')[-1],'wb') as f:
f.write(resp.content)
print('写入图片中')
else:
print('文件已存在')
最后看下我的结果: