背景:

萧大在bilibili的直播使用js制作游戏的课程视频质量比较高(入口),鉴于 bilibili 上的视频在线观看时无法加速,现在想还不如自己下载到本地观看实在。遂使用 pyquery,requests,配合  you-get 写了一个小脚本来自动下载视频到指定的目录,需本机已安装you-get。

代码如下:

# /usr/bin/env python
# -*- coding:utf-8 -*-

import os
import requests
from pyquery import PyQuery as pq


def get_urls(url):
    """
    解析网页数据,获得目标url
    """
    response = requests.get(url)
    if response.status_code == 200:
        # print(response.text)
        html = response.text
        doc = pq(html)
        a_list = doc('li.video a.title')
        # print(a_list)
        print(type(a_list))
        url_list = [a.attr('href').strip('//') for a in a_list.items()]
        print(url_list)
        return url_list
    return None


def cmd_download(url):
    """
    逐条进行下载视频
    """
    try:
        info = os.system(r'you-get --debug -o C:\test  {}'.format(url))
        print(info)
    except Exception as e:
        print(e)
        cmd_download(url)


def main():
    """
    函数的主入口
    """
    for i in range(1, 3):
        url = 'https://search.bilibili.com/video?keyword=%E8%90%A7%E4%BA%95%E9%99%8C&order=totalrank&page=' + str(i)
        url_list = get_urls(url)
        if url_list is not None:
            [cmd_download(url) for url in url_list]


if __name__ == '__main__':
    main()
测试结果如下:



Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐