抓取lol全英雄圖(不含皮膚)


需要使用到的庫

  • requests

  • json

  • os

  • re

 

打開  “http://lol.qq.com/web201310/info-heros.shtml”  

 

查看 url 

 

 

實現方法:通過  “

http://lol.qq.com/biz/hero/champion.js

”提取出每個英雄對應的ID,再通過每個英雄對應的ID 拼接 鏈接 獲得圖片地址,最后保存在本地文件夾。

 

源代碼如下:

 1 import requests
 2 import json
 3 import re
 4 import os
 5 
 6 class Grab_img(object):
 7     hero_img_list = []
 8 
 9     def __init__(self,url):
10         self.url = url
11 
12 
13     def grab_id(self):
14         response = requests.get(self.url)
15         pat = r"\"keys\":(.+),\"data\""
16         hero_id = json.loads(str(re.findall(pat, response.text))[2:-2])
17         for i in hero_id:
18             self.hero_img_list.append('http://ossweb-img.qq.com/images/lol/web201310/skin/big' + i + '000.jpg')
19 
20 
21 
22     def storage_img(self):
23         for i in self.hero_img_list:
24             root = "F://爬取圖片存儲//"
25             # 文件名
26             path = root + i.split("/")[-1]
27 
28             # 判斷根目錄是否存在
29             if not os.path.exists(root):
30                 os.mkdir(root)
31 
32             # 判斷文件是否存在
33             if not os.path.exists(path):
34                 r = requests.get(i)
35                 with open(path, "wb") as f:
36                     f.write(r.content)
37                     print("文件保存成功~~")
38             else:
39                 print("文件已存在~~")
40 
41 
42     def main(self):
43         #獲取英雄ID
44         self.grab_id()
45 
46         #下載保存圖片
47         self.storage_img()
48 
49 
50 if __name__ == '__main__':
51     try:
52         t = Grab_img("http://lol.qq.com/biz/hero/champion.js")
53         t.main()
54     except:
55         print("爬取出錯啦~~~")

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM