数据来源: 英雄联盟官网
开发环境:win10、python3.7
开发工具:pycharm
爬虫获取官网所有皮肤数据,爬虫比较简单不做过多介绍
爬虫分析:
获取所有英雄id数据
根据id请求英雄详情json数据获取所有英雄图片数据
对图片发送异步请求
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : BaiChuan
# @File : lol_skin_spider.py
import requests
import asyncio
import aiohttp
import time
class Crawl_Image:
def __init__(self):
self.url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
self.url1 = "https://game.gtimg.cn/images/lol/act/img/js/hero/{}.js"
self.path = r'E:\python_project\vip_course\lol_video\skins'
self.headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
}
async def get_image(self, url):
'''异步请求库aiohttp 加快图片 url 的网页请求'''
async with aiohttp.ClientSession() as session:
response = await session.get(url)
content = await response.read()
return content
async def download_image(self, image):
html = await self.get_image(image[0])
with open(self.path + "\\" + image[1] + '.jpg', 'wb') as f:
f.write(html)
print('下载第{}张图片成功'.format(image[1]))
def run(self):
hero_list = requests.get(self.url, headers=self.headers).json()
print(hero_list)
for hero in hero_list['hero']:
heroId = hero['heroId']
skins = requests.get(self.url1.format(heroId)).json()['skins']
task = [asyncio.ensure_future(self.download_image((skin['mainImg'], skin['name']))) for skin in skins]
loop = asyncio.get_event_loop()
# 执行协程
loop.run_until_complete(asyncio.wait(task))
if __name__ == '__main__':
crawl_image = Crawl_Image()
crawl_image.run()
该爬虫基于协程的异步加入会有超时异常的报错,直接捕获就好了
超时异常处理
捕捉就好了…基本上碰到的有这些异常
asyncio.TimeoutError
aiohttp.client_exceptions.ServerDisconnectedError
aiohttp.client_exceptions.InvalidURL
aiohttp.client_exceptions.ClientConnectorError
将全部的图片通过cv2合成
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : BaiChuan
# @File : make_video.py
import cv2
import os
import numpy as np
video_dir = 'result.mp4'
# 帧率
fps = 1
# 图片尺寸
img_size = (1920, 1080)
fourcc = cv2.VideoWriter_fourcc('m','p', '4.jpg', 'v') # 视频编码
videoMake = cv2.VideoWriter(video_dir, fourcc, fps, img_size) # 创建视频
img_files = os.listdir(r'E:\python_project\vip_course\lol_video\skins')
for i in img_files:
img_path = r'E:\python_project\vip_course\lol_video\skins' + '\\' + i
print(img_path)
frame = cv2.imdecode(np.fromfile(img_path,dtype=np.uint8),-1) # 读取进制数据
print(frame)
frame = cv2.resize(frame, img_size) # 生成视频
videoMake.write(frame) # 写进视频里
videoMake.release() # 释放资源
大功告成,图片的播放速度可根据帧率调整
最后一步,给视频配上音效
声音的添加需要FFmpeg工具可在裙里获取
资料获取学习沟通裙:731685275
import subprocess
inmp4 = 'result.mp4'
inmp3 = 'Legends_Never_Die.mp3'
save_data = r'E:\python_project\vip_course\lol_video\data.mp4'
cmd=f'ffmpeg -i {inmp4} -i {inmp3} -codec copy ' + save_data
subprocess.call(cmd, shell=True)
大功告成!!!!!
视频放置B站:各位大大可自行观看!!! 原声视频: 视频
Selenium 是什么? Selenium是一个用于Web应用程序测试的工具,可以模拟真正的用...
本文将介绍一段实例代码,来讲解利用正则表达式使C#判断输入日期格式是否正确的...
之前做了一个淘宝客返利微信公众号,后来很多人提到过微信返利机器人,现在微信...
原著:Jan Egil Refsnes 翻译:阿捷 七. XSL 的控制语句 1.条件语句if...then XS...
首先需要注意的是,本文即将提到的 Druid,并非阿里巴巴的 Druid 数据库连接池,...
在HTML中常用以下3种方式定义CSS:Embedding(嵌入式)、Linking(引用式)、Inl...
第一种方法:通过URL链接地址传递 send.aspx: protected void Button1_Click( se...
本文转载自网络,原文链接:https://mp.weixin.qq.com/s/-Eftrp2yKh3-bPrD5NX2lQ...
本文实例讲述了Thinkphp5框架中引入Markdown编辑器操作。分享给大家供大家参考,...
4.XPath的语法 我们在前面已经提到过,XPath是用来帮助XSLT在XML源文档中查找定...