• python下载油管视频


    import time
    from pytube import YouTube
    import requests
    # 需要自行创建三个文件夹 video audio cover
    
    def getId(str):
        vid = str.split("=")[-1]
        print(vid)
        return vid
    
    
    def down(vid):
        if not vid:
            print("地址有误,请重试")
            return
        start = time.time()
        yt = YouTube('https://youtu.be/%s' % vid)
        itags = [137, 399, 22]
        for i in itags:
            try:
                yt.streams.get_by_itag(i).download(output_path="./video", filename_prefix=vid + "_")
                if i != 22:
                    yt.streams.get_by_itag(140).download(output_path="./audio", filename_prefix=vid + "_")
                break
            except:
                print("换清晰度..")
        end = time.time()
        downCover(vid)
        print("下载成功,用时%s秒" % (end - start))
    
    
    def downCover(vid):
        try:
            # 1080p
            res = requests.get("https://img.youtube.com/vi/%s/maxresdefault.jpg" % vid)
            with open("./cover/%s.jpg" % vid, 'wb') as f:
                f.write(res.content)
        except:
            # 720p
            res = requests.get("https://img.youtube.com/vi/%s/hqdefault.jpg" % vid)
            with open("./cover/%s.jpg" % vid, 'wb') as f:
                f.write(res.content)
    
    
    if __name__ == '__main__':
        URL = "https://www.youtube.com/watch?v=J4Ws9A-K14I"
        vid = getId(URL)
        down(vid)
    人生苦短,慢慢潇洒。 nsyw.run
  • 相关阅读:
    JavaWeb_Tomcat_Maven!
    java异常!
    JavaWeb获取web.xml初始参数!getInitParameter
    JavaWeb文件下载!
    Java抽象接口!
    JavaWeb初识Servlet!
    关于HashMap以对象作为Key的实现及踩坑
    浅析.Net下的多线程编程(2)
    获取浏览器分辨率
    浅析.Net下的多线程编程(1)
  • 原文地址:https://www.cnblogs.com/zwnsyw/p/15657864.html
Copyright © 2020-2023  润新知