玩王者技术太菜,或许读者可以看看小编的这篇文章

tech2022-09-19  62

原标题:运用Python爬虫下载王者荣耀英雄讲解视频

前言:

王者官网上的英雄讲解视频的确不错,但是不知道读者知不知道这个玩意啊!但愿知道吧!不过,即使不知道,看了小编的这篇文章也就知道了,注意:部分视频用小编的那个代码无法下载,希望大家谅解!

文章目录

原标题:运用Python爬虫下载王者荣耀英雄讲解视频1.完成这个需要的Python模块和.exe文件2.怎样得到视频的下载链接3.完整实现整个操作4.运行结果视频和参考代码5.总结

1.完成这个需要的Python模块和.exe文件

需要的Python模块有selenium、requests、lxml、json、os。 讲到selenium,小编想就必须提到一个.exe文件,我用的是谷歌浏览器,即为chromedriver.exe,这个下载就不讲解了,不懂得读者可以看看小编得这篇文章,python下载网易云音乐,这篇文章里面小编讲到了这个.exe文件具体下载。

2.怎样得到视频的下载链接

视频的下载链接怎样得到呢? 也就是运用上面提到的那个selenium模块,我们来到这个界面,

网页为:https://pvp.qq.com/v/detail.shtml?G_Biz=18&tid=643431,按电脑键盘的F12键,可以发现这个下面有这个视频的下载链接,显然这个下载链接是动态加载的,所以要用到selenium模块。 怎样提取这个视频下载链接呢?当然是用xpath语法呐! 参考代码如下:

from selenium import webdriver url='https://pvp.qq.com/v/detail.shtml?G_Biz=18&tid=643431' driver=webdriver.Chrome() driver.get(url=url) driver.implicitly_wait(30) print(driver.find_elements_by_xpath('.//txpdiv[@class="txp_video_container"]/video')[0].get_attribute('src')) driver.close()

运行完成后,可以在运行结果中发现如下结果: 这就是这个视频的下载链接,点击进去就知道了。

3.完整实现整个操作

怎样根据自己的需要选择自己想了解的英雄呢?本来我是想爬取所有英雄链接和名字的,这个网址为:https://pvp.qq.com/web201605/herolist.shtml

但是,有了一个问题,那名字不知道是被加密了,还是怎的,运行结果是这样的。 代码为:

from lxml import etree import requests url='https://pvp.qq.com/web201605/herolist.shtml' headers={ 'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3756.400 QQBrowser/10.5.4039.400'} response=requests.get(url=url,headers=headers) html=etree.HTML(response.text) L2=html.xpath('.//ul[@class="herolist clearfix"]/li/a') for i in range(len(L2)): print(L2[i].xpath('./text()'))

后面发现network下面的xhr里面有一个网址是这个的,虽然看到的和这个运行结果一样,但是用代码加载一下,就是原来的那个效果了。 代码运行结果: 代码为:

import requests import json url='https://pvp.qq.com/web201605/js/herolist.json' headers={ 'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3756.400 QQBrowser/10.5.4039.400'} response=requests.get(url=url,headers=headers) dict2=json.loads(response.text) print(dict2)

现在,英雄的名字我们得到了,那么这个链接怎样得到呢?其实在这个json文件中,那个链接也已经得到了,只不过需要字符串拼接一下。 拼接成功后的这个网址,点击进去,就是这样 现在需要做的就是来到观看视频的界面,也就是点击上图中这个播放视频的符号,来到另外一个界面。 之后的操作就是第二个标题那里讲到的,小编就不一一讲解了。

4.运行结果视频和参考代码

运行结果:

运用Python爬虫下载王者荣耀英雄讲解视频

参考代码如下:

from selenium import webdriver from lxml import etree import requests import json import os class wangZhe(object): def __init__(self): self.headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36'} self.url1 = 'https://pvp.qq.com/web201605/js/herolist.json' # 网址一,存储英雄列表信息 self.url2='https://pvp.qq.com/web201605/herodetail/%d.shtml' # 网址二 def getNames(self): # 定义一个方法,用于提取网址一中的英雄相关信息的数据 response = requests.get(url=self.url1, headers=self.headers) str1 = response.text L2 = json.loads(str1) # 列表类型 for i in range(len(L2)): print('{}----{}'.format(i+1, L2[i]['cname']), end='\t\t') if (i+1) % 4 == 0: print() id=input('请输入想看的视频序号(具体格式为 1,2,5): ') id=id.split(',') id=[int(i) for i in id] L3=[[L2[i-1]['ename'] for i in id],[L2[i-1]['cname'] for i in id]] return L3 def getInfo(self): L2=self.getNames() L3=L2[0] L4=list() for i in range(len(L3)): response=requests.get(url=self.url2%(L3[i]),headers=self.headers) html=etree.HTML(response.text) L4.append(html.xpath('.//a[@class="hero-video"]/@href')[0]) L5=[L4,L2[1]] return L5 def mkDir(self): # 创建文件夹 xpath=input('请输入创建文件夹的路径:') try: os.mkdir(xpath) except Exception as e: print('错误原因:{}'.format(e)) return xpath def writeDir(self,file,url): # 写入文件 response=requests.get(url=url,headers=self.headers) with open(file=file,mode='wb') as f: f.write(response.content) def downloadVideo(self): L4=self.getInfo() L5=L4[0] url='https:%s'%(L5[0]) driver=webdriver.Chrome() driver.get(url=url) driver.implicitly_wait(30) L6=list() for i in range(1,len(L5)): url='https:%s'%(L5[i]) driver2=webdriver.Chrome() driver2.get(url) downUrl=driver.find_elements_by_xpath('.//txpdiv[@class="txp_video_container"]/video')[0].get_attribute('src') L6.append(downUrl) driver.implicitly_wait(30) driver.close() driver=driver2 downUrl=driver.find_elements_by_xpath('.//txpdiv[@class="txp_video_container"]/video')[0].get_attribute('src') L6.append(downUrl) driver.close() # 下面是写入文件操作 path=self.mkDir() L7=L4[1] for i in range(len(L6)): if 'mp4' not in L6[i]: print('{}.mp4暂不支持下载!'.format(L7[i])) else: self.writeDir(file=path+'./{}.mp4'.format(L7[i]),url=L6[i]) print('{}.mp4下载成功!'.format(L7[i])) if __name__ == "__main__": a=wangZhe() a.downloadVideo()

5.总结

这个程序还有一些不足,比如很少用try 和except ,也许有个时候就报错了,另外,也没有用到ip代理,如果有读者觉得小编的这篇文章还不错的话!记得点赞喔!

最新回复(0)