20 lines of Python code crawling the king's glory all hero skin

~wangweijun 2020-11-13 07:21:32
lines python code crawling king


introduction

The glory of the king has been played by everyone , I should have heard of those who haven't played , As the hottest mobile phone in the world MOBA game , Cough , It seems to be out of the question . Our focus today is to crawl all the skin of the king's glory all the heroes , And just use 20 That's ok Python The code is done .

preparation

It's not hard to get the skin itself , The difficulty lies in the analysis of , We have to get the skin picture first url Address , Don't talk much , We will come to the official website of the king's glory :
 Insert picture description here
Let's click on the hero profile , Then choose a hero at will , next F12 Turn on the debugging table , Find the image address of the hero's original skin :
 Insert picture description here
next , Let's switch the hero's skin , You will find that there is no significant change in the image address , It's just that the last number has changed , Let's compare the addresses of the two skin pictures :

http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/523/523-bigskin-1.jpg
http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/523/523-bigskin-2.jpg

We can guess , For the same hero's skin image address , It's just that the last number is different , To prove our conjecture , We can continue to find a full skin image of a hero , Find one with a little more skin , For example, what I am looking for here is sun Shangxiang , Put all of its skin image addresses together and compare :

http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/111/111-bigskin-1.jpg
http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/111/111-bigskin-2.jpg
http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/111/111-bigskin-3.jpg
http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/111/111-bigskin-4.jpg
http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/111/111-bigskin-5.jpg
http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/111/111-bigskin-6.jpg
http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/111/111-bigskin-7.jpg

So we come to the conclusion that , The same hero's skin image path from 1 Start increasing in sequence , Let's take a look at how different heroes are distinguished . Will find , No matter how the skin image changes , The address above the browser is always the same , So we'll have two different heroes url Let's compare the addresses :

https://pvp.qq.com/web201605/herodetail/523.shtml
https://pvp.qq.com/web201605/herodetail/111.shtml

At first glance , There seems to be no rule , But we're going to find something here , The last number actually controls which hero , We think it's the number of the hero , Unfortunately , There seems to be no rule between hero numbers , There's no need to worry , Let's go to the official website to find the clues .

In the hero profile interface , We turn on F12 Debugging station , By grabbing network requests , I found several documents :
 Insert picture description here
Click on the network , And then click XHR, You can see these documents , You should know the name of the document , These files store the hero list information , Let's click to see :
 Insert picture description here
you 're right , What's stored here is hero information , Including the hero's name , Hero number and other information , We can try the accuracy of the information , For example, Joe's ename, That is, the hero number is 106, So as I thought before , The detailed address of hero Joe should be :https://pvp.qq.com/web201605/herodetail/106.shtml
After trying, I found that it was true .

Come here , The preparation is done , In fact, it's here , The whole project is half finished , Next is the implementation of the code .

Code implementation

First let's create one Python file , Then import os and requests modular .
Follow the previous steps , We need to get the hero list information first , That is to say herolist.json file , The file address is :https://pvp.qq.com/web201605/js/herolist.json, This can be found in the debugging table .
So we need to get the hero list information through this address first json data , And then parse json data , Extract useful information :

url = 'https://pvp.qq.com/web201605/js/herolist.json'
herolist = requests.get(url) # Get a list of Heroes json file 
herolist_json = herolist.json() # Turn into json Format 
hero_name = list(map(lambda x: x['cname'], herolist.json())) # Pick up the hero's name 
hero_number = list(map(lambda x: x['ename'], herolist.json())) # Extract the hero's number 

So we get the hero's name and number , You can test the output :
With the hero number , It's easy , Just splice it url address :
http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' + hero_number + '/' + hero_number + '-bigskin-1.jpg, In this way, you can get the skin pictures of all heroes , But there's a problem , How little skin a hero has , Some heroes have only two skins , There are six or seven , So we don't know the maximum image number , Here I take a more stupid approach , It's about having a variable from 1 To 10 Add in sequence to the mosaic image address , We don't deal with any pictures we don't have , Because no hero has more skin than 10 individual , So we can get all the pictures . Now let's look at the code implementation :

# Download the pictures 
def downloadPic():
i = 0
for j in hero_number:
# Create folder 
os.mkdir("C:\\Users\\Administrator\\Desktop\\wzry\\" + hero_name[i])
# Enter the created folder 
os.chdir("C:\\Users\\Administrator\\Desktop\\wzry\\" + hero_name[i])
i += 1
for k in range(10):
# Splicing url
onehero_link = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' + str(j) + '/' + str(
j) + '-bigskin-' + str(k) + '.jpg'
im = requests.get(onehero_link) # request url
if im.status_code == 200:
open(str(k) + '.jpg', 'wb').write(im.content) # write file 

The implementation is very simple , The code comments have also been written clearly , With this function , We just need to call , You can download the pictures , The complete code of the whole program is as follows :

import os
import requests
url = 'https://pvp.qq.com/web201605/js/herolist.json'
herolist = requests.get(url) # Get a list of Heroes json file 
herolist_json = herolist.json() # Turn into json Format 
hero_name = list(map(lambda x: x['cname'], herolist.json())) # Pick up the hero's name 
hero_number = list(map(lambda x: x['ename'], herolist.json())) # Extract the hero's number 
# Download the pictures 
def downloadPic():
i = 0
for j in hero_number:
# Create folder 
os.mkdir("C:\\Users\\Administrator\\Desktop\\wzry\\" + hero_name[i])
# Enter the created folder 
os.chdir("C:\\Users\\Administrator\\Desktop\\wzry\\" + hero_name[i])
i += 1
for k in range(10):
# Splicing url
onehero_link = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' + str(j) + '/' + str(
j) + '-bigskin-' + str(k) + '.jpg'
im = requests.get(onehero_link) # request url
if im.status_code == 200:
open(str(k) + '.jpg', 'wb').write(im.content) # write file 
downloadPic()

Remove comments , near 20 Line of code we completed the king glory all hero skin crawling , Isn't it very simple ? We can test this program , First, create a folder on the desktop , be known as wzry, Because I've written the code here , If you want to modify it, you can also modify it , After creating the folder, click Run , Wait for a moment , All the pictures are downloaded .
 Insert picture description here
 Insert picture description here
 Insert picture description here
For the program json String parsing , We can also use jsonpath Module to do , Using this module can get the information we want more quickly , The parsing method is as follows :

hero_name = jsonpath.jsonpath(html_json, "$..cname")
hero_number = jsonpath.jsonpath(html_json, "$..ename")

The method receives one json String and parsing rules ,$…cname It means to find any location from the root directory cname For the value of the bond , And put it in the dictionary .

ending

Reptiles are very interesting , Because it's very intuitive , Strong visual impact , It also has a sense of achievement , Reptiles are powerful , But don 't crawl through the privacy information .

Last , If there are better suggestions for the procedures in this article , Leave a comment in the comments section .

版权声明
本文为[~wangweijun]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database