Python爬取LOL英雄皮肤

Python 爬虫 

一 实现分析

  1. 在官网上找到英雄皮肤的真实链接,查看多个后发现前缀相同,后面对应为英雄的ID和皮肤的ID,皮肤的ID从00开始顺序递增,而英雄ID跟网页中的顺序无关,需要找到英雄ID。
  2. 并没有在皮肤页面和英雄页面的元素中找到有关英雄ID的内容,所以想到有可能是通过js文件加载进来的。通过Chrome工具找到跟英雄ID有关的js文件网址。
  3. 通过js文件网址获得英雄ID。与图片真实网址进行拼接,得到所有英雄的所有皮肤的图片地址,保存到列表中。
  4. 设置图片保存的路径和文件名。
  5. 下载。

二 知识点与难点

  1. 通过re库来正则表达式的处理。
  2. 通过json将字符串转成字典类型。
  3. 图片地址和文件名称的拼接。
  4. 判断图片是否真实存在和下载。

三 代码

  1. '''

  2. 1. 找到图片路径,获取所有图片真实URL

  3. 2. 设置文件名

  4. 3. 下载

  5. '''


  6. import requests 

  7. import re 

  8. import json 


  9. def getLOLImages():


  10. # 包含英雄名字和ID的js文件路径

  11. js_url = "http://lol.qq.com/biz/hero/champion.js"


  12. # 获取js文件内容

  13. js_content = requests.get(js_url).text 


  14. # 截取需要的内容 .*? 代表所有内容

  15. req = '"keys":(.*?),"data"'

  16. # 取到的是列表,真正想要的是列表中的第一个元素

  17. js_want = re.findall(req, js_content)[0] 


  18. # 转成字典形式

  19. js_dict = json.loads(js_want) 


  20. # 获取图片的真实URL,并保存到列表中

  21. # http://ossweb-img.qq.com/images/lol/web201310/skin/big266000.jpg

  22. pic_url_list = [] 


  23. for hero_id in js_dict: 

  24. for skin_id in range(20): 

  25. skin_id = str(skin_id) 

  26. if len(skin_id) == 1: 

  27. num_str = '00' + skin_id 

  28. elif len(skin_id) == 2: 

  29. num_str = '0' + skin_id 


  30. pic_url = "http://ossweb-img.qq.com/images/lol/web201310/skin/big" + hero_id + num_str +".jpg"


  31. pic_url_list.append(pic_url) 


  32. # 设置文件名称

  33. path = "D://lol/"

  34. path_file_list = [] 

  35. for pic_name in js_dict.values(): 

  36. for skin_id in range(20): 

  37. skin_id = str(skin_id) 

  38. if len(skin_id) == 1: 

  39. num_str = '00' + skin_id 

  40. elif len(skin_id) == 2: 

  41. num_str = '0' + skin_id 


  42. path_file = path + pic_name + num_str + ".jpg"

  43. path_file_list.append(path_file) 


  44. # 下载

  45. n = 0

  46. for dl_url in pic_url_list: 

  47. # n += 1

  48. res = requests.get(dl_url) 

  49. if res.status_code == 200: 

  50. print("正在下载{}".format(path_file_list[n])) 

  51. with open(path_file_list[n], "wb") as f: 

  52. f.write(res.content) 


  53. n += 1



  54. if __name__ == '__main__': 

  55. getLOLImages() 

 


Python爬取LOL英雄皮肤的更多相关文章

  1. Python3爬虫使用requests爬取lol英雄皮肤

    本人博客:https://xiaoxiablogs.top 此次爬取lol英雄皮肤一共有两个版本,分别是多线程版本和非多线程版本. 多线程版本 # !/usr/bin/env python # -*- ...

  2. Python爬取 | 王者荣耀英雄皮肤海报

    这里只展示代码,具体介绍请点击下方链接. Python爬取 | 王者荣耀英雄皮肤海报 import requests import re import os import time import wi ...

  3. 利用python爬取王者荣耀英雄皮肤图片

    前两天看到同学用python爬下来LOL的皮肤图片,感觉挺有趣的,我也想试试,于是决定来爬一爬王者荣耀的英雄和皮肤图片. 首先,我们找到王者的官网http://pvp.qq.com/web201605 ...

  4. Python 爬取所有51VOA网站的Learn a words文本及mp3音频

    Python 爬取所有51VOA网站的Learn a words文本及mp3音频 #!/usr/bin/env python # -*- coding: utf-8 -*- #Python 爬取所有5 ...

  5. python爬取网站数据

    开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...

  6. python爬取某个网页的图片-如百度贴吧

    python爬取某个网页的图片-如百度贴吧 作者:vpoet mail:vpoet_sir@163.com 注:随意copy,不用告诉我 #coding:utf-8 import urllib imp ...

  7. Python:爬取乌云厂商列表,使用BeautifulSoup解析

    在SSS论坛看到有人写的Python爬取乌云厂商,想练一下手,就照着重新写了一遍 原帖:http://bbs.sssie.com/thread-965-1-1.html #coding:utf- im ...

  8. 使用python爬取MedSci上的期刊信息

    使用python爬取medsci上的期刊信息,通过设定条件,然后获取相应的期刊的的影响因子排名,期刊名称,英文全称和影响因子.主要过程如下: 首先,通过分析网站http://www.medsci.cn ...

  9. python爬取免费优质IP归属地查询接口

    python爬取免费优质IP归属地查询接口 具体不表,我今天要做的工作就是: 需要将数据库中大量ip查询出起归属地 刚开始感觉好简单啊,毕竟只需要从百度找个免费接口然后来个python脚本跑一晚上就o ...

随机推荐

  1. pcl point merge

    http://pointclouds.org/documentation/tutorials/pairwise_incremental_registration.php#pairwise-increm ...

  2. [Schema] I have updated my XML Schema for my service but SoapUI still generates/validates according to the old schema.

    SoapUI caches XML schemas when they are first loaded. If you need to force a reload of an interfaces ...

  3. Log4j配置(转)

    原文:http://www.blogjava.net/zJun/archive/2006/06/28/55511.html Log4J的配置文件(Configuration File)就是用来设置记录 ...

  4. Qt的QPixmap半透明

    QPixmap pix1(":/PixmapTest/Resources/Chrysanthemum.jpg"); QPixmap temp(pix1.size()); temp. ...

  5. mybatis 简单实现 left join

    sql -- 表a ta (id,aname,bid) -- 表b tb (id,bname) SELECT T1.*, T2.BNAME FROM TA T1 LEFT JOIN TB T2 ON ...

  6. 获取cpu频率的代码

    taskset是linux自带的一个命令,可用来将进程绑定到指定CPU 相关的函数有: sched_setaffinity, CPU_CLR, CPU_ISSET, CPU_SET, CPU_ZERO ...

  7. 部署图像深度学习Web网站

    1. 内网穿透 2. 深度学习Web化 https://www.cnblogs.com/haolujun/p/9778939.html

  8. Class Loading Deadlocks

    By tomas.nilsson on Feb 28, 2010 Mattis keeps going strong, in this installment you get to learn eve ...

  9. Activity Fragment转场动画

    Activity转场动画 先介绍个动画的好例子:https://github.com/lgvalle/Material-Animations Activity的转场动画是通过overridePendi ...

  10. 2、Semantic-UI之网格布局

    2.1 网格布局   在semantic-ui中提供了16个网格,使用class="column",当然也可以通过数字来表示当前网格大小.   在Semantic-UI中定义的网格 ...