Python 自动爬取B站视频

文件名自定义(文件格式为.py),脚本内容:

#!/usr/bin/env python
#-*-coding:utf-8-*-
import requests
import random
import time


def get_json(url):
    headers = {
        'User-Agent': 
        'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
    }

    params = {
        'page_size': 10,
        'next_offset': str(num),
        'tag': '今日热门',
        'platform': 'pc'
    }
    try:
        html = requests.get(url,params=params,headers=headers)
        return html.json()
        
    except BaseException:
        print('request error')
        pass
        
def download(url,path):
    start = time.time() # 开始时间
    size = 0
    headers = {
        'User-Agent': 
        'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
    }
    
    response = requests.get(url,headers=headers,stream=True) # stream属性必须带上
    chunk_size = 1024 # 每次下载的数据大小
    content_size = int(response.headers['content-length']) # 总大小
    if response.status_code == 200:
        print('[文件大小]:%0.2f MB' %(content_size / chunk_size / 1024)) # 换算单位
        with open(path,'wb') as file:
            for data in response.iter_content(chunk_size=chunk_size):
                file.write(data)
                size += len(data) # 已下载的文件大小



if __name__ == '__main__':
    for i in range(10):
        url = 'http://api.vc.bilibili.com/board/v1/ranking/top?'
        num = i*10 + 1
        html = get_json(url)
        infos = html['data']['items']
        for info in infos:
            title = info['item']['description'] # 小视频的标题
            video_url = info['item']['video_playurl'] # 小视频的下载链接
            print(title)


            # 为了防止有些视频没有提供下载链接的情况
            try:
                download(video_url,path='videos/%s.mp4' %title)
                print('成功下载一个!')
            except BaseException:
                print('凉凉,下载失败')
                pass
        time.sleep(int(format(random.randint(2,8)))) # 设置随机等待时间

爬取效果如下:
1571891-20181229102038200-301341217.png

爬取的文件:
1571891-20181229102611854-897565474.png

生成一个windows平台可执行exe程序
工具安装:pip install PyInstaller
生成exe程序:
pyinstaller -i test.ico -F Grasp.py

打包过程:
1571891-20181229154219495-22321441.png

打包好的文件:
1571891-20181229154833708-814180732.png

参数含义:
1571891-20181229154722992-2110616139.png

程序:
1571891-20181229154907317-432262138.png

百度云下载链接

链接:百度云
提取码:hqhr

脚本内容收集自互联网,不保证生成的程序时效

转载于:https://www.cnblogs.com/ArchitecTang/p/10194249.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值