python

您所在的位置:网站首页 英雄联盟所有英雄头像名字 python

python

2024-01-30 05:43| 来源: 网络整理| 查看: 265

python -又一次爬虫练习(爬取LOL所有的英雄头像)

目标网站:https://lol.qq.com/data/info-heros.shtml#Navi

一开始我尝试用requests来get网页源代码,结果发现不管用。 查看一下源代码,发现

在这里插入图片描述原来全部是用JS脚本动态加载出来的,直接在源码里面找是找不到的。

这就要祭上我们的 Web kit 了

Web kit的安装与使用请看上一篇:https://blog.csdn.net/weixin_44823747/article/details/108173246

直接上代码

源码: # coding=utf-8 import re from urllib.request import urlopen import os.path import sys from PyQt4.QtWebKit import * from PyQt4.QtGui import * from PyQt4.QtCore import * class Render(QWebPage): # 用来渲染网页,将url中的所有信息加载下来并存到一个新的框架中 def __init__(self,url): self.app = QApplication(sys.argv) QWebPage.__init__(self) self.loadFinished.connect(self._loadFinished) self.mainFrame().load(QUrl(url)) self.app.exec_() def _loadFinished(self, result): self.frame = self.mainFrame() self.app.quit() #创建images文件夹 images_dir = 'images/' if not os.path.exists(images_dir): os.mkdir(images_dir) url = 'https://lol.qq.com/data/info-heros.shtml#Navi' r = Render(url) #得到经过渲染的网页 html = r.frame.toHtml() #截取主体部分 body_search = re.search(r'([\s\S]+?).*?',html) body = body_search.group(1) #获取图片链接和对应英雄名 hero = re.findall(r'(.*?)\s(.*?).*?',body) #写入图片 for i in hero: name = i[1] + '_' + i[2] print("正在下载"+name) image = urlopen('http:' + i[0]) with open('images/'+name+'.png','wb') as f: f.write(image.read()) print("----下载完成----")

发现还挺简单的,也就三四十行代码

在这里插入图片描述 简简单单。。。

爬取所有物品也是同样的原理,这个代码改一下就能实现,由于本人比较懒,所以就不搞了。。。

在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3