好看視頻是百度旗下的短視頻平臺,里面有海量好看的短視頻,遇到我們喜歡的,要如何下載呢。
下面直接給出python語言中地址的解析及各種不同格式視頻的下載。
#encoding:utf-8 # 好看視頻下載 import socket from urllib.request import urlopen import urllib import re import time from pyquery import PyQuery as pq import requests from tqdm import tqdm # 打印進度條的庫 import gzip print('程序開始運行。。。') requests.adapters.DEFAULT_RETRIES = 5 # connect to a URL timeout = 30 socket.setdefaulttimeout(timeout)#這里對整個socket層設置超時時間。后續文件中如果再使用到socket,不必再設置 sleep_download_time = 3 time.sleep(sleep_download_time) #這里時間自己設定 # 輸入好看視頻地址 haokanurl = input('請輸入要下載的好看視頻的網頁地址:') # haokanurl = 'https://haokan.baidu.com/v?vid=7448757459481911514&tab=yinyue_new'#示例地址 #為了避免出現403提示,這里偽裝瀏覽器 headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'} req = urllib.request.Request(url=haokanurl, headers=headers) website = urlopen(req,timeout = 90) print('好看視頻下載地址解析中') # read html code html = website.read().decode('UTF-8') #html = website.read().decode() #當使用上面的直接decode()出錯時可以使用下面的方法 # html = website.read() # buff = BytesIO(html) # f = gzip.GzipFile(fileobj=buff) # html = f.read().decode('utf-8') website.close() # use re.findall to get all the links # 取得視頻名稱(標題) videotitle = re.findall('<h1 class="videoinfo-title">(.*?)</h1>',html)[0] print(videotitle) # 默認地址 links = re.findall('"playurl":"(.*)","clarityUrl"',html)#默認地址 downurl = links[0] downurl = downurl.replace('\\','') print('視頻實際地址:') print(downurl) print('現在開始下載該視頻,請稍等。。。') res = requests.get(downurl, headers={'user-agent': 'chrome'}) total_size = round(int(res.headers["Content-Length"])/1024/1024) print('解析完成,視頻大小為:' + str(total_size) + 'MB。現在開始下載。') with open(f'{videotitle}down.mp4', 'wb') as f: for chunk in tqdm(iterable=res.iter_content(1024*1024), total=total_size, unit='KB'):\ f.write(chunk) print('下載完成。') sdlinks = re.findall('"key":"sd","rank":0,"title":"(.*?)","videoBps":',html)[0]#標清地址 sdlinks = re.findall('"url":"(.*)',sdlinks) sdurl = sdlinks[0] sdurl = sdurl.replace('\\','') print('標清視頻地址是:') print(sdurl) print('現在開始下載標清視頻,請稍等。。。') res = requests.get(sdurl, headers={'user-agent': 'chrome'}) total_size = round(int(res.headers["Content-Length"])/1024/1024) print('解析完成,視頻大小為:' + str(total_size) + 'MB。現在開始下載。') with open(f'{videotitle}標清.mp4', 'wb') as f: for chunk in tqdm(iterable=res.iter_content(1024*1024), total=total_size, unit='KB'):\ f.write(chunk) print('下載完成。') hdlinks = re.findall('"key":"hd","rank":1,"title":"(.*?)","videoBps":',html)[0]#高清地址 hdlinks = re.findall('"url":"(.*)',hdlinks) hdurl = hdlinks[0] hdurl = hdurl.replace('\\','') print('高清視頻地址是:') print(hdurl) print('現在開始下載高清視頻,請稍等。。。') res = requests.get(hdurl, headers={'user-agent': 'chrome'}) total_size = round(int(res.headers["Content-Length"])/1024/1024) print('解析完成,視頻大小為:' + str(total_size) + 'MB。現在開始下載。') with open(f'{videotitle}高清.mp4', 'wb') as f: for chunk in tqdm(iterable=res.iter_content(1024*1024), total=total_size, unit='KB'):\ f.write(chunk) print('下載完成。') sclinks = re.findall('"key":"sc","rank":2,"title":"(.*?)","videoBps":',html)[0]#超清地址 sclinks = re.findall('"url":"(.*)',sclinks) scurl = sclinks[0] scurl = scurl.replace('\\','') print('超清視頻地址是:') print(scurl) print('現在開始下載超清視頻,請稍等。。。') res = requests.get(scurl, headers={'user-agent': 'chrome'}) total_size = round(int(res.headers["Content-Length"])/1024/1024) print('解析完成,視頻大小為:' + str(total_size) + 'MB。現在開始下載。') with open(f'{videotitle}超清.mp4', 'wb') as f: for chunk in tqdm(iterable=res.iter_content(1024*1024), total=total_size, unit='KB'):\ f.write(chunk) print('下載完成。') p1080links = re.findall('"key":"1080p","rank":3,"title":"(.*?)","videoBps":',html)[0]#藍光地址 p1080links = re.findall('"url":"(.*)',p1080links) p1080url = p1080links[0] p1080url = p1080url.replace('\\','') print('藍光視頻地址是:') print(p1080url) print('現在開始下載藍光視頻,請稍等。。。') res = requests.get(p1080url, headers={'user-agent': 'chrome'}) total_size = round(int(res.headers["Content-Length"])/1024/1024) print('解析完成,視頻大小為:' + str(total_size) + 'MB。現在開始下載。') with open(f'{videotitle}藍光.mp4', 'wb') as f: for chunk in tqdm(iterable=res.iter_content(1024*1024), total=total_size, unit='KB'):\ f.write(chunk) print('下載完成。') print('所有格式視頻下載完成,請檢查是否正確。')