|
| 1 | +# encoding: utf-8 |
| 2 | +# create cookie.txt into which you need to put the link including BV number. |
| 3 | +# single catch version |
| 4 | +# usage: change the BV nubmer and header in this code and run, you can choose the quality of download video. |
| 5 | +from lxml import etree |
| 6 | +import requests |
| 7 | +import os |
| 8 | +# 获取视频名称 |
| 9 | +def name(bv,headers): |
| 10 | + url = 'https://www.bilibili.com/video/'+bv |
| 11 | + text = requests.get(url,headers = headers).text |
| 12 | + tree = etree.HTML(text) |
| 13 | + name = tree.xpath('//*[@id="viewbox_report"]/h1/span/text()')[0] |
| 14 | + return name |
| 15 | +# 获取cid |
| 16 | +def cid(bv,headers): |
| 17 | + url = 'https://api.bilibili.com/x/player/pagelist' |
| 18 | + param = { |
| 19 | + 'bvid':'%s'%bv, |
| 20 | + 'jsonp':'jsonp' |
| 21 | + } |
| 22 | + text = requests.get(url,params = param,headers = headers).json() |
| 23 | + cid = text['data'][0]['cid'] |
| 24 | + return cid |
| 25 | +# 获取视频url |
| 26 | +def flv(cid,bv,headers,quality): |
| 27 | + url = 'https://api.bilibili.com/x/player/playurl' |
| 28 | + param = { |
| 29 | + 'cid':'%s'%cid, |
| 30 | + 'bvid':'%s'%bv, |
| 31 | + 'qn':'%s'%quality, |
| 32 | + } |
| 33 | + text = requests.get(url,params = param,headers = headers).json() |
| 34 | + return text |
| 35 | +# 请求视频并保存 |
| 36 | +def get_flv(name,flv_url,headers): |
| 37 | + print("\n等待响应数据(需要的时间较长)...") |
| 38 | + response = requests.get(flv_url,headers = headers) |
| 39 | + code = response.status_code |
| 40 | + print('\n响应码:',code) |
| 41 | + text = response.content |
| 42 | + if code == 200: |
| 43 | + with open("./%s.flv"%name,'wb') as fp1: |
| 44 | + fp1.write(text) |
| 45 | + with open("./list.txt",'a') as fp2: |
| 46 | + fp2.write(name+"\n"+bv+"\n\n") |
| 47 | + print("视频获取成功\n(若视频清晰度不符,请及时更新cookie值)") |
| 48 | + else: |
| 49 | + print("视频获取失败") |
| 50 | +# main |
| 51 | +bv = 'BV1TA411A7V6'#input("输入BV号:") |
| 52 | +# 获取本地txt保存的cookie |
| 53 | +cookie = "" |
| 54 | +if os.path.exists("./cookie.txt"): |
| 55 | + with open('./cookie.txt') as fp: |
| 56 | + cookie = fp.read() |
| 57 | + cookie = cookie.split('\n')[0] |
| 58 | +print('\ncookie:',cookie) |
| 59 | +headers = { |
| 60 | + 'Referer':'https://www.bilibili.com/', |
| 61 | + 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36', |
| 62 | + 'cookie':"%s"%cookie, |
| 63 | +} |
| 64 | +cid = cid(bv,headers) |
| 65 | +print('\n可获取cookie值的链接(需先在浏览器正常登录)\nhttps://api.bilibili.com/x/player/playurl?cid=%s&bvid=%s'%(cid,bv)) |
| 66 | +name = name(bv,headers) |
| 67 | +print('\n标题:',name) |
| 68 | +print('\ncid:',cid) |
| 69 | +quality = '' |
| 70 | +text =flv(cid,bv,headers,quality) |
| 71 | +qn = text['data']['support_formats'] |
| 72 | +print("\n可选择的清晰度(部分清晰度可能获取失败):") |
| 73 | +for qu in qn: |
| 74 | + print(('清晰度:%s'%qu['new_description']).ljust(15)+('视频质量参数:%d'%qu['quality']).ljust(15)+('格式参数:%s'%qu['format']).ljust(15)) |
| 75 | +quality = input("输入清晰度对应的视频质量参数(默认1080p):") |
| 76 | +if quality == '': |
| 77 | + quality = '80' |
| 78 | +text = flv(cid,bv,headers,quality) |
| 79 | +flv_url = text['data']['durl'][0]['url'] |
| 80 | +print('\nflv_url:',flv_url) |
| 81 | +get_flv(name,flv_url,headers) |
0 commit comments