selenium抓取虎牙短视频

今天闲着没事,用selenium抓取视频保存到本地,只爬取了第一页,只要小于等于5分钟的视频。。。

为什么不用requests,没有为什么,就因为有些网站正则和xpath都提取不出来想要的东西,要么就是接口出来的数据加密,要么就因为真正的视频url规律难找!

selenium几行代码轻轻松松就搞定!

 

安装selenium库,设置无界面模式,以及驱动下载详情见我的另一篇blog:https://www.cnblogs.com/pfeiliu/p/12275239.html

 

代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import requests
#设置无界面模式
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

class VideoCrawl(object):
    video_box=[]#收集video真正的url
    def __init__(self,url):
        self.driver=webdriver.Chrome(executable_path=r"C:\Program Files\python\Lib\site-packages\selenium\webdriver\chrome\chromedriver.exe",options=chrome_options)#设置无界面模式
        self.driver.get(url)

    #程序运行完毕,析构函数关闭selenium
    def __del__(self):
        print("爬取结束。。。。。",len(VideoCrawl.video_box),VideoCrawl.video_box)
        self.driver.close()

    def run(self):
        self.get_detail_info()
    #获取列表页所有详情页的url
    def get_detail_info(self):
        detail_info = self.driver.find_elements_by_xpath('//a[@class="video-wrap statpid"]')
        detail_url=[]
        for i in detail_info:
            detail_url.append(i.get_attribute('href'))#获取视频页url
        video_playtime_list=self.driver.find_elements_by_xpath('//span[@class="video-duration"]')
        video_playtime_list=[i.text for i in video_playtime_list]
        for res in zip(detail_url,video_playtime_list):
            playtime=res[1].split(":")[0]
            # print("playtime--------",playtime)
            if int(res[1].split(":")[0])<=5:#播放时间小于5分钟的要
                # print(res[0],"解析的url",playtime)
                self.parse_video(res[0],res[1])
            else:
                pass
    #解析详情页
    def parse_video(self,url,t):
        self.driver.get(url)
        videoobj = self.driver.find_elements_by_xpath('//video')
        video_url=videoobj[0].get_attribute('src')
        title=self.driver.find_elements_by_xpath('//h1[@class="video-title"]')[0].text
        print('video_url--------',video_url,title,t)
        #保存video到本地
        self.save_video(video_url,title,t)
        #类变量统计video_url
        VideoCrawl.video_box.append(video_url)
    #保存,请求video_url,二进制保存为mp4
    def save_video(self,url,title,t):
        filename="video"+title+"-"+t.replace(":","")+".mp4"
        video=requests.get(url).content
        with open(filename,"wb") as file:
            file.write(video)
        print(f"{filename}写入文件完毕")

if __name__ == '__main__':
    crawl=VideoCrawl('https://v.huya.com/cat/7')
    crawl.run()

运行结果如下:

"C:\Program Files\python\python.exe" C:/Users/Administrator.SC-201903160419/Desktop/note/exer/myapp.py
video_url-------- https://huya-w10.huya.com/2005/265917310/1300/d973823b0f437c9d78fc40b9691fdb54.mp4 【轩子小剧场】最意外的自行车 04:23
video【轩子小剧场】最意外的自行车-0423.mp4写入文件完毕
video_url-------- https://huya-w10.huya.com/2006/267302224/1300/f8a363ec243e4adb2857491f695bc118.mp4 轩子巨2兔:轩子教你演戏 05:06
video轩子巨2兔:轩子教你演戏-0506.mp4写入文件完毕
video_url-------- https://huya-w6.huya.com/2005/264805062/1300/582b726b05db31fc12a1e5557011a6bf.mp4 【麦秀彩儿】跳个舞吧 05:58
video【麦秀彩儿】跳个舞吧-0558.mp4写入文件完毕
video_url-------- https://huya-w10.huya.com/2005/264956230/1300/97fa603f7b174ec30c19013f894bd108.mp4 轩子小剧场:你的女仆请签收 01:18

Process finished with exit code -1

 

  都可以正常播放。。。

切记:自己娱乐下练练手删了即可,千万不要用于商业用途哦!

posted @ 2019-11-23 00:19  腹肌猿  阅读(1133)  评论(0编辑  收藏  举报