python在B站爬糖豆廣場舞

先附上代碼:

import requests,re
from lxml  import etree

#這是單頁面下載,翻頁的目前還不會 url
= 'https://search.bilibili.com/all?vt=96737335&keyword=%E7%B3%96%E8%B1%86%E5%B9%BF%E5%9C%BA%E8%88%9E' headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36', 'referer':'https://www.bilibili.com/' } res = requests.get(url,headers=headers) # print(res.text) html = etree.HTML(res.text) links = html.xpath('//*[@id="i_cecream"]/div/div[2]/div[2]/div/div/div/div[2]/div/div[*]/div/div[2]/div/div/a/@href') # print(links) for link in links: link = 'https:'+ link # print(link) url_res = requests.get(link,headers=headers) url_html = etree.HTML(url_res.text) #視頻名 title = url_html.xpath('//*[@id="viewbox_report"]/h1/text()')[0] title2 = re.sub('\W','',title) # print(title2) # 獲取音頻URL get_ad = re.findall(r'"id":30280,"baseUrl":"(.*?)","base_url"',url_res.content.decode()) get_ad2=[] if not get_ad: # print('列表爲空') continue else: # print(get_ad[0]) get_ad2 = get_ad # get_ad = re.findall(r'"id":17,"baseUrl":"(.*?)","base_url"',url_res.content.decode()) #獲取視頻url get_vd = re.findall(r'"id":32,"baseUrl":"(.*?)","base_url"',url_res.content.decode()) get_vd2 = [] if not get_vd: continue else: get_vd2 = get_vd # print(get_vd2) # with open('log2.txt','wb') as f: # f.write(get_vd2) headers2 = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36', 'referer':'https://www.bilibili.com', } # 第二次發送請求,請求視頻 # print('1111') res_ad = requests.get(get_ad2[0],headers=headers2) res_vd = requests.get(get_vd2[0],headers=headers2) # res2 = res_ad.content print('1111') with open(f'video/{title2}ad.mp4','wb') as f: f.write(res_ad.content) with open(f'video/{title2}vd.mp4','wb') as f: f.write(res_vd.content) print(f'{title2}視頻下載完成!') from moviepy.editor import * vd = VideoFileClip(f"video/{title2}vd.mp4") au = AudioFileClip(f"video/{title2}ad.mp4") vd2 = vd.set_audio(au) vd2.write_videofile(f"video/{title2}.mp4")

下載完以後的成果。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章