python批量下载bilibili视频_python 下载bilibili视频

1 importrequests2 importre3 importos4 importjson5 importsys6 importmath7 from lxml importetree8

9

10 classBLDSplider:11 regex_cid = re.compile("\"cid\":(.{8})")12

13 def __init__(self, aid):14 self.aid =aid15

16 self.origin_url = "https://www.bilibili.com/video/av{}?from=search&seid=9346373599622336536".format(aid)17 self.headers ={18 "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",19 }20

21 self.url = "https://api.bilibili.com/x/player/playurl?avid={}&cid={}&qn=0&type=&otype=json"

22

23 defcheck_dir(self, author_name):24 #检查目录

25 self.parent_path = "e:/bilibili/" + author_name + "/" + str(self.aid) + "/"

26 if notos.path.exists(self.parent_path):27 os.makedirs(self.parent_path)28

29 self.video_name = self.parent_path + str(self.aid) + ".mp4"

30

31 defparse_url(self, item):32 cid = item["cid"]33 print("aid:%s cid:%s" %(str(self.aid), cid))34 title = item["title"]35 print("title:%s" %title)36

37 self.headers["Referer"] =self.origin_url38 #视频

39 response = requests.get(self.url.format(self.aid, cid), headers=self.headers)40 if response.status_code == 200:41 result =json.loads(response.content.decode())42 durl = result["data"]["durl"][0]43 video_url = durl["url"]44 print("video_url:%s" %video_url)45 #视频大小

46 size = durl["size"]47 print("size:%s,约%2.2fMB" % (size, size / (1024 * 1024)))48 video_response = requests.get(video_url, headers=self.headers, stream=True)49 if video_response.status_code == 200:50 with open(self.video_name, "wb") as file:51 buffer = 1024

52 count =053 whileTrue:54 if count + buffer <=size:55 file.write(video_response.raw.read(buffer))56 count +=buffer57 else:58 file.write(video_response.raw.read(size %buffer))59 count += size %buffer60 file_size =os.path.getsize(self.video_name)61 #print("\r下载进度 %.2f %%" % (count * 100 / size), end="")

62

63 width = 50

64 percent = (count /size)65 use_num = int(percent *width)66 space_num = int(width -use_num)67 percent = percent * 100

68 print('\r进度:[%s%s] %d%%' % (use_num * '#', space_num * ' ', percent), file=sys.stdout,69 flush=True, end="")70 if size ==count:71 break

72 print("\r\n")73

74 #获取视频相关参数

75 defget_video_info(self):76 response = requests.get(self.origin_url, headers=self.headers)77 item =dict()78 if response.status_code == 200:79 #author

80 html_element =etree.HTML(response.content.decode())81 author =dict()82 author_name =html_element.xpath(83 "/html/body/div[@id='app']/div[@class='v-wrap']/div[@class='r-con']/div[@id='v_upinfo']//a[@report-id='name']/text()")[84 0]85 #通常是微博,微信公众号等联系方式

86 author_others =html_element.xpath(87 "/html/body/div[@id='app']/div[@class='v-wrap']/div[@class='r-con']/div[@id='v_upinfo']//div[@class='desc']/@title")[88 0]89 author["name"] =author_name90 author["others"] =author_others91 item["author"] =author92

93 #cid

94 cid =BLDSplider.regex_cid.findall(response.content.decode())[0]95 item["cid"] =cid96 info_url = "https://api.bilibili.com/x/web-interface/view?aid={}&cid={}".format(self.aid, cid)97 info_response = requests.get(info_url, headers=self.headers)98 if info_response.status_code == 200:99 data = json.loads(info_response.content.decode())["data"]100 #视频简介

101 desc = data["desc"]102 item["desc"] =desc103

104 #title

105 title = data["title"]106 item["title"] =title107

108 stat = data["stat"]109 #播放量

110 view = stat["view"]111 item["view"] =view112

113 #弹幕

114 danmaku = stat["danmaku"]115 item["danmaku"] =danmaku116

117 #评论

118 reply = stat["reply"]119 item["reply"] =reply120

121 #硬币

122 coin = stat["coin"]123 item["coin"] =coin124

125 #点赞

126 like = stat["like"]127 item["like"] =like128

129 #收藏

130 favorite = stat["favorite"]131 item["favorite"] =favorite132

133 #分享

134 share = stat["share"]135 item["share"] =share136 self.check_dir(item["author"]["name"])137 #视频参数

138 with open(self.parent_path + "video_info.txt", "w") as file:139 file.write(json.dumps(item, ensure_ascii=False, indent=2))140 returnitem141

142 defrun(self):143 item =self.get_video_info()144 self.parse_url(item)145

146

147 defmain():148 #55036734

149 aid = 55036734

150 if len(sys.argv) >= 2:151 if sys.argv[1]:152 aid = sys.argv[1]153 splider =BLDSplider(aid)154 splider.run()155

156

157 if __name__ == '__main__':158 main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值