今天想和大家做一个爬虫,来获取英雄联盟里面的皮肤图片。
所需要的函数库有json re(正则表达式库) requests和time
其中只有requests需要通过cmd命令行安装,安装方法就是在cmd命令行输入下面字符
pip Install requests
import json import re import requests import time
def getLOLImages(): header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'} url_js = 'http://lol.qq.com/biz/hero/champion.js' #获取JS源代码 Str bytes res_js = requests.get(url_js).content #转码 转成字符串 html_js = res_js.decode() #正则表达式 req = '"keys":(.*?),"data"' list_js = re.findall(req,html_js) #转成dict dict_js = json.loads(list_js[0]) # print(type(dict_js)) #定义图片列表 pic_list = [] for key in dict_js: # print(key)#英雄ID
第一步是获取英雄ID从而为先面判决URL作准备。
ID的获取地址是从英雄联盟官网F12后在Network里找到champion.js,具体地址如下
http://lol.qq.com/biz/hero/champion.js
for i in range(20): number = str(i) if len(number) == 1: hero_num = "00"+number elif len(number) == 2: hero_num = "0"+number numstr = key+hero_num url = "http://ossweb-img.qq.com/images/lol/web201310/skin/big"+numstr+".jpg" #http://ossweb-img.qq.com/images/lol/web201310/skin/big81000.jpg pic_list.append(url)
第二步就是拼接URL了,通过发现英雄皮肤url的取名方式,我们可以方向最后的数字是不同的。让后通过此方法来获取图片地址。
list_filepath = [] path = "D:\Pycharmdaima\Pachong\LOLTU\\" for name in dict_js.values(): for i in range(20): file_path = path+name+str(i)+'.jpg' list_filepath.append(file_path)
第三步是获取图片名称,path那行是放置图片的地址,注意结尾的\\不能丢。
n = 0 for picurl in pic_list: res = requests.get(picurl) n += 1 #获取状态码 if res.status_code == 200: print("正在下载%s"%list_filepath[n]) time.sleep(1) with open(list_filepath[n],'wb') as f: f.write(res.content)
第四步就是下载图片了,注释掉time.sleep()后会加速爬取速度 但是可能被腾讯中断爬取。
下面是全部代码:
import json import re import requests import time #获取JS源代码 获取英雄ID #拼接URL地址 #获取图片下载地址 #下载图片 #驼峰命名法 #获取英雄图片 def getLOLImages(): header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'} url_js = 'http://lol.qq.com/biz/hero/champion.js' #获取JS源代码 Str bytes res_js = requests.get(url_js).content #转码 转成字符串 html_js = res_js.decode() #正则表达式 req = '"keys":(.*?),"data"' list_js = re.findall(req,html_js) #转成dict dict_js = json.loads(list_js[0]) # print(type(dict_js)) #定义图片列表 pic_list = [] for key in dict_js: # print(key)#英雄ID #拼接URL for i in range(20): number = str(i) if len(number) == 1: hero_num = "00"+number elif len(number) == 2: hero_num = "0"+number numstr = key+hero_num url = "http://ossweb-img.qq.com/images/lol/web201310/skin/big"+numstr+".jpg" #http://ossweb-img.qq.com/images/lol/web201310/skin/big81000.jpg pic_list.append(url) #获取图片名称 list_filepath = [] path = "D:\Pycharmdaima\Pachong\LOLTU\\" for name in dict_js.values(): for i in range(20): file_path = path+name+str(i)+'.jpg' list_filepath.append(file_path) #下载图片 n = 0 for picurl in pic_list: res = requests.get(picurl) n += 1 #获取状态码 if res.status_code == 200: print("正在下载%s"%list_filepath[n]) time.sleep(1) with open(list_filepath[n],'wb') as f: f.write(res.content) getLOLImages()