One move will take you to master all the videos of station B. Python script will download your favorite fairy videos and download whatever you want

Posted by knowj on Sat, 22 Jan 2022 16:27:51 +0100

Mobile phone buddies, especially those with little sisters, are now being written out to download B's dance videos. Now, interested friends can try to practice their hands, and then download other areas such as animation, music, fashion, ghost and tiktok, etc. after they master the method, B can help them to learn how to do the work. Download whatever you want.

1, Grab list

First click the dance area and select the house dance list.

Then open the control panel of F12 to find one https://api.bilibili.com/x/web-interface/newlist?rid=20&type=0&pn=1&ps=20&jsonp=jsonp&callback=jsonCallback_bili_57905715749828263 Where rid is the small category of station B and PN is the number of pages.

Xiaobian tried to open the address in the browser and reported 404, but the return value of this address in the control panel is clearly the video list. Try to remove the callback parameter and get the desired result unexpectedly.

As we all know, bid is the unique ID of a station B video. To obtain bid, you can extract aid from the return value of the url above, and then convert aid into bid.

Str = 'fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF'  # Prepared string of specified strings
Dict = {}
  
# Put each character of the string into the dictionary for one-to-one correspondence, for example, f corresponds to 0, Z corresponds to 1, and so on.
for i in range(58):
    Dict[Str[i]] = i
  
s = [11, 10, 3, 8, 4, 6, 2, 9, 5, 7]  # Necessary decryption list
xor = 177451812 
add = 100618342136696320  # This string of numbers is finally subtracted or added
  
def algorithm_enc(av):
    ret = av
    av = int(av)
    av = (av ^ xor) + add
    # Convert the format of BV number (BV + 10 characters) into a list to facilitate subsequent operations
    r = list('BV          ')
    for i in range(10):
        r[s[i]] = Str[av // 58 ** i % 58]
    return ''.join(r)
  
  
def find_bid(p):
    bids = []
    r = requests.get(
            'https://api.bilibili.com/x/web-interface/newlist?&rid=20&type=0&pn={}&ps=50&jsonp=jsonp'.format(p))
       
    data = json.loads(r.text)
    archives = data['data']['archives']
  
    for item in archives:
        aid = item['aid']
        bid = algorithm_enc(aid)
        bids.append(bid)
  
    return bids

2, Get CID of video

To download 1080 videos, bid alone is not enough. You also need the SESSDATA value and cid in the Cookie after login.
First, log in to station B and copy the SESSDATA in the Cookie to the object header. The address is https://api.bilibili.com/x/player/pagelist?bvid= The URL returns the cid.

def get_cid(bid):
    url = 'https://api.bilibili.com/x/player/pagelist?bvid=' + bid
  
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
        'Cookie': 'SESSDATA=182cd036%2C1636985829%2C3b393%2A51',
        'Host': 'api.bilibili.com'
    }
  
    html = requests.get(url, headers=headers).json()
  
    infos = []
  
    data = html['data']
    cid_list = data
    for item in cid_list:
        cid = item['cid']
        title = item['part']
        infos.append({'bid': bid, 'cid': cid, 'title': title})
    return infos

3, Download Video

Download Video https://api.bilibili.com/x/player/playurl From the recommendation list after each video playback.

Finally, use urllib request. The urlretrieve function downloads the video.

```python
def get_video_list(aid, cid, quality):
    url_api = 'https://api.bilibili.com/x/player/playurl?cid={}&bvid={}&qn={}'.format(cid, aid, quality)
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
        'Cookie': 'SESSDATA=182cd036%2C1636985829%2C3b393%2A51',
        'Host': 'api.bilibili.com'
    }
    html = requests.get(url_api, headers=headers).json()
    video_list = []
  
    for i in html['data']['durl']:
        video_list.append(i['url'])
    return video_list
  
def schedule_cmd(blocknum, blocksize, totalsize):
    percent = 100.0 * blocknum * blocksize/ totalsize
    s = ('#' * round(percent)).ljust(100, '-')
    sys.stdout.write('%.2f%%' % percent + '[' + s + ']' + '\r')
    sys.stdout.flush()
  
def download(video_list, title, bid):
    for i in video_list:
        opener = urllib.request.build_opener()
        opener.addheaders = [
            ('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'),
            ('Accept', '*/*'),
            ('Accept-Language', 'en-US,en;q=0.5'),
            ('Accept-Encoding', 'gzip, deflate, br'), 
            ('Range', 'bytes=0-'),  
            ('Referer', 'https://www.bilibili.com/video/'+bid),
            ('Origin', 'https://www.bilibili.com'),
            ('Connection', 'keep-alive'),
  
        ]
  
        filename=os.path.join('D:\\video', r'{}_{}.mp4'.format(bid,title)) 
  
        try:
            urllib.request.install_opener(opener)
            urllib.request.urlretrieve(url=i, filename=filename, reporthook=schedule_cmd) 
        except:
            print(bid + "Download exception, file:" + filename)

Thank you for watching my article carefully. The following is what I share with you purely. Reciprocity. You can see the Xiaobian home page for more wonderful Python content.

Directly click to receive 🎁🎁🎁🎁🎁

① Part time communication, industry consultation and online professional answers of big men
② Python development environment installation tutorial
③ Python 400 set self-study video
④ Common vocabulary of software development
⑤ Python learning Roadmap
⑥ More than 3000 Python e-books

If you can use it, you can take it away directly. As long as you have it, you won't hide it. I'll take the private letter, or in my QQ technical exchange group, group number: 675240729 (pure technical exchange and resource sharing, advertising is not allowed) to take it away by yourself.

Topics: Python Programming Programmer Deep Learning NLP