import os
import re
import requests
from lxml import etree

class Livideo(object):
    def href(self):
        # 获取该网页所有的源码
        response = requests.get('http://www.pearvideo.com/category_8')
        # 对得到的源码进行解码
        html = etree.HTML(response.content.decode())
        # 获取所有视频的链接
        href_list = html.xpath('//*[@id="categoryList"]/li/div/a/@href')
        # 遍历所有的视频链接
        for href in href_list:
            self.video(href)

    def video(self, href):
        path = r"D:\梨视频_python\\"
        # 获取每个视频的源码
        response = requests.get(r'http://www.pearvideo.com/' + href)
        # 用正则去匹配所有以http开头,以mp4结尾的内容
        result = re.findall(re.compile("http.+mp4"), response.text)
        # 判断当前目录下是否有该文件夹,如果没有则创建该文件夹
        if not os.path.exists(path):
            os.mkdir(path)
        # 拼接出文件路径
        file_path = path + href + ".mp4"
        # 得到每个视频的二进制数据
        res = requests.get(*result).content
        print('正在下载视频:{};保存到:{}'.format(*result, file_path))
        # 写入文件
        with open(file_path, 'wb') as f:
            f.write(res)

livideo = Livideo()
livideo.href()
Last modification:July 6th, 2020 at 06:33 pm
如果觉得我的文章对你有用,请随意赞赏