小项目:python3爬虫抓取王者荣耀所有英雄皮肤原图
Python环境:python3.6.4
author : Chris iven
效果图:
代码:
class WZRY_Hero(object): Origin_url = "http://pvp.qq.com/web201605/" def __init__(self,url): self.url = url def get_hero_info(self): res = requests.get(self.url) with open("herodata.json", "w", encoding="utf-8") as f: f.write(json.dumps(res.text)) # JSON.LOADS() 将文本转换为json对象 # JSON.DUMPS() 将json对象转换为文本 with open("herodata.json", "r", encoding="utf-8") as f: str = json.loads(f.read()) data2 = json.loads(str) hero_number = [] hero_name = [] for i in data2: hero_number.append(i.get("ename")) hero_name.append(i.get("cname")) #print(hero_name, '\n', hero_number) return hero_number, hero_name def write_to_mysql(self,url,): db = pymysql.connect("localhost","root","123456","game_skin","charset") pass def download_pic(self,url_number,name): i = 0 while i < len(url_number): #//game.gtimg.cn/images/yxzj/img201606/skin/hero-info/169/169-bigskin-2.jpg j = 1 while j < 8: old_url = "http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/"+str(url_number[i])+"/"+str(url_number[i])+"-bigskin-"+str(j)+".jpg" #new_url = old_url+"-bigskin-"+str(j)+".jpg" print(name[i]+'的下载的链接是:',old_url) response = requests.get(old_url,timeout=10) if "404 page not found" in response.text: print("网页错误,无法打开!!!") break else: try: os.mkdir("王者荣耀各英雄的皮肤/"+name[i]) except FileExistsError: pass print(name[i],"已经创建!!!") with open("王者荣耀各英雄的皮肤/"+name[i]+"/"+str(j) + ".jpg", "wb")as f: f.write(response.content) print(name[i]+"已经下载完毕!!!") j+=1 i+=1 def Main(self): number ,name = self.get_hero_info() self.download_pic(number,name) if __name__ == "__main__": url = "http://pvp.qq.com/web201605/js/herolist.json" wz = WZRY_Hero(url) wz.Main()
难度: *
大家可以尝试着交换思路,反正我是没找到皮肤的原名....