├── Bilibili_fav_downloader.py └── README.md /Bilibili_fav_downloader.py: -------------------------------------------------------------------------------- 1 | import urllib.request 2 | import re 3 | import sys 4 | from subprocess import call 5 | 6 | 7 | class Bili_fav: 8 | 9 | video_ids = [] 10 | video_titles = [] 11 | 12 | # get url address 获取收藏夹地址 13 | def __init__(self, user_id, favorites_id): 14 | self.favurl ="https://api.bilibili.com/x/space/fav/arc?vmid={}&fid={}".format(user_id, favorites_id) 15 | 16 | # load videos addresses 读取视频地址 17 | def load_favorites(self): 18 | page = 1 19 | while True: 20 | response = urllib.request.urlopen(self.favurl+"&pn={}".format(page)) 21 | content = response.read().decode('utf-8') 22 | id_results = re.findall('"aid":(\d+),', content) 23 | title_results = re.findall('"highlight_title":"(.*?)"', content) 24 | 25 | if not id_results: 26 | break 27 | else: 28 | page += 1 29 | 30 | # store titles and videos ids 31 | self.video_titles += title_results 32 | for video_id in id_results: 33 | if video_id not in self.video_ids: 34 | self.video_ids.append(video_id) 35 | 36 | # report spider results 显示爬虫结果 37 | def report(self): 38 | print("已搜索到%s部视频:" % len(self.video_ids)) 39 | i = 1 40 | for title in self.video_titles: 41 | print("%s."%i + title) 42 | i += 1 43 | 44 | # 下载视频 45 | def download_videos(self, output_dir = ''): 46 | i = 1 47 | if output_dir == '': 48 | for video_id in self.video_ids: 49 | print("第%s/%s部视频下载中:(使用 ctrl+c 暂停下载)" % (i, len(self.video_ids))) 50 | call("you-get --playlist https://www.bilibili.com/video/av{}".format(video_id), shell=True) 51 | i += 1 52 | else: 53 | for video_id in self.video_ids: 54 | print("第%s/%s部视频下载中:(使用 ctrl+c 暂停下载)" % (i, len(self.video_ids))) 55 | call("you-get --playlist -o {} https://www.bilibili.com/video/av{}".format(output_dir, video_id), shell=True) 56 | i += 1 57 | 58 | 59 | if __name__ == '__main__': 60 | 61 | # 请输入用户id和收藏夹id 62 | user_id = 111111 63 | favorites_id = 11111 64 | # 请输入下载目录 65 | output_dir = "/Usr/...." 66 | 67 | my_fav = Bili_fav(user_id, favorites_id) 68 | my_fav.load_favorites() 69 | my_fav.report() 70 | my_fav.download_videos(output_dir) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bilibili 收藏夹下载器 2 | 3 | 基于 you-get 实现一键下载Bilibili指定收藏夹下的视频。 4 | 5 | ## 环境要求 6 | 7 | - `Python3` 与 `urllib` 库 8 | - 基于 python 开发的视频下载器 `you-get` ( https://github.com/soimort/you-get ) 9 | 10 | 如果已经安装有 `python 3`,可直接通过命令行安装 `you-get`。 11 | 12 | ``` 13 | pip3 install you-get 14 | ``` 15 | 16 | ## 使用方法 17 | 18 | 打开 Bilibili_fav_downloader.py ,编辑用户id,收藏夹id与下载目录,之后直接运行即可。 19 | 20 | ### 如何查看用户id与收藏夹id 21 | 22 | 进入任意一个B站收藏夹,如 https://space.bilibili.com/32708543/favlist?fid=2935220 。 23 | 24 | 其地址中,`32708543` 这一部分即为用户id,`2935220` 则为收藏夹id。 25 | 26 | ### 注意事项 27 | 28 | 1. `you-get` 会自动下载收藏夹内每个视频的所有分P。 29 | 2. 如果下载文件夹中已经存在下载好的视频文件,`you-get` 会自动跳过。 30 | 3. B站收藏夹必须被设置成公共,私有收藏夹无法下载。 31 | --------------------------------------------------------------------------------