前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 自动爬取B站视频

Python 自动爬取B站视频

作者头像
公众号---人生代码
发布2019-07-24 14:51:21
9570
发布2019-07-24 14:51:21
举报
文章被收录于专栏:人生代码人生代码人生代码

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)))) # 设置随机等待时间

生成一个windows平台可执行exe程序

工具安装:

pip install PyInstaller

生成exe程序:

pyinstaller -i test.ico -F Grasp.py

打包过程:

下载

百度云下载: https://pan.baidu.com/s/1Tf6fgjXhB-njJSECcnO5Pw
提取码 : 2epc

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 CryptoCode 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Python 自动爬取B站视频
    • 工具安装:
      • 生成exe程序:
        • 打包过程:
          • 下载
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档