preface
Before coming off work, see a group of people in the discussion with king pesticide some skin as computer wallpaper, hd, what pixels lower, the one hand, Internet also has, but the pixel is different, so, I think, to climb website directly his hd skin good, then there is the theme of this article said.
Climbing figure thinking
Find the heroes list
Enter the official website, then enter the hero introduction, view more heroes, you can see all heroes, which is the link below
List of heroes: pvp.qq.com/web201605/h…
Details of the hero
Click on each hero, you can see the detailed information, basic introduction and skin display of each hero. The skin we need to crawl is in the lower right corner, put the mouse on it, you can show the skin one by one
More information about Xiao Lu Ban: pvp.qq.com/web201605/h…
Analyze the skin image URL
Img SRC and data-imgname SRC and data-imgName are attributes of img. If you look at them, you can see that the attribute value of SRC is the small image. Imgname is the id of the hero, and 112 is the id of the hero. The id of the hero is 112, and 112 is the id of the hero. The 2 in Bigskin-2 represents the number of skin images of the hero
Start writing the crawler script
Step 1: Define some common variables
Step 2: Grab a list of all heroes
Step 3: Loop through, analyzing each hero skin node
Step 4: Download the image
Step 5: End of crawler
Complete source code
Feel the above seven seven eight eight of, said some of what ah, really ink, not as directly on the code is true, well, I was wrong, immediately hand over the source code, please forgive each hero woman, at the same time, code I also uploaded a dating website GitHub.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""Grab King of Glory Skin author: Gxcuizy Date: 2018-11-06"""
import requests
from bs4 import BeautifulSoup
from urllib import parse
import os
class Skin(object):
def __init__(self):
# Json data for heroes
self.hero_url = 'https://pvp.qq.com/web201605/js/herolist.json'
# Common URL prefix information for hero detail page
self.base_url = 'https://pvp.qq.com/web201605/herodetail/'
# hero detail page URL suffix information
self.detail_url = ' '
# Images store folder
self.img_folder = 'skin'
# generic prefix for image URLS
self.skin_url = 'https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/'
The suffix of the image URL
self.skin_detail_url = ' '
def get_hero(self):
"""Get hero JSON data"""
request = requests.get(self.hero_url)
hero_list = request.json()
return hero_list
def get_hero_skin(self, hero_name, hero_no):
"""Get details page hero skin display information and climb the map"""
url = parse.urljoin(self.base_url, self.detail_url)
request = requests.get(url)
request.encoding = 'gbk'
html = request.text
Node to get skin information
soup = BeautifulSoup(html, 'lxml')
skip_list = soup.select('.pic-pf-list3')
for skin_info in skip_list:
Get the skin name
img_names = skin_info.attrs['data-imgname']
name_list = img_names.split('|')
skin_no = 1
# Loop to download skin images
for skin_name in name_list:
self.skin_detail_url = '%s/%s-bigskin-%s.jpg' % (hero_no, hero_no, skin_no)
skin_no += 1
img_name = hero_name + The '-' + skin_name + '.jpg'
self.download_skin(img_name)
def download_skin(self, img_name):
"""Download skin image"""
img_url = parse.urljoin(self.skin_url, self.skin_detail_url)
request = requests.get(img_url)
if request.status_code == 200:
print('download-%s' % img_name)
img_path = os.path.join(self.img_folder, img_name)
with open(img_path, 'wb') as img:
img.write(request.content)
else:
print('img error! ')
def make_folder(self):
"""Create image Store folder"""
if not os.path.exists(self.img_folder):
os.mkdir(self.img_folder)
def run(self):
"""Script Execution Entry"""
self.make_folder()
hero_list = self.get_hero()
for hero in hero_list:
hero_no = str(hero['ename'])
self.detail_url = hero_no + '.shtml'
hero_name = hero['cname']
self.get_hero_skin(hero_name, hero_no)
Program execution entry
if __name__ == '__main__':
skin = Skin()
skin.run()
Copy the code
The last
In fact, the idea is so simple, of course, if there are other ideas and ideas, welcome to leave a message. Oh, almost forgot, if you are interested, you can try to climb the league of Legends all hero skin hd pictures, any other questions, also welcome to leave a message and exchange.