python爬虫:获取b站某个up的视频aid、cid

获取B站up的全部视频aid、cid 代码比较乱,请轻喷

import requests
import json

def get_json(UserUid):
    # UserUid = '2026561407'
    # up主主页url
    url_data = 'https://api.bilibili.com/x/space/acc/info?mid=' + UserUid + '&jsonp=jsonp'
    # 视频集合页url
    url_video = 'https://api.bilibili.com/x/space/arc/search?mid=' + UserUid + '&pn=1&ps=25&index=1&jsonp=jsonp'
    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63'}
    # ---------------获取数据------------------------
    data = requests.get(url=url_data, headers=headers)
    video = requests.get(url=url_video, headers=headers)

    # ---------------转化数据-------------------------
    data_info = json.loads(data.text)
    video_info = json.loads(video.text)
    # ---------------数据获取-------------------------
    Uid = data_info["data"]["mid"]
    Name = data_info["data"]["name"]
    Video_AID = []
    for i in range(10):
        # Video = info3["data"]["list"]["vlist"]
        Video_title = video_info["data"]["list"]["vlist"][i]["title"]
        Video_play = video_info["data"]["list"]["vlist"][i]["play"]
        Video_review = video_info["data"]["list"]["vlist"][i]["video_review"]
        Video_aid = video_info["data"]["list"]["vlist"][i]["aid"]
        # print(type(Video_aid))
        Video_AID.append(Video_aid)
        # print("-----%s的B站第%s个视频信息-----"%(Name,i))
        # print("\tUid:%s"%Uid)
        # print("\tVideo_title:%s\n"
        #       "\t播放量:%s\n"
        #       "\t评论数:%s\n"
        #       "\t视频aid:%s"
        #       %(Video_title, Video_play, Video_review, Video_aid)
        # )
    return Video_AID

def get_cid(Video_aid):
    Video_CID = []
    for i in Video_aid:
        url = "https://api.bilibili.com/x/player/pagelist?aid=" + str(i) + "&jsonp=jsonp"
        headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63'}
        v_data = requests.get(url=url, headers=headers)
        v_cid_info = json.loads(v_data.text)
        video_cid = v_cid_info["data"][0]["cid"]
        Video_CID.append(video_cid)
        # print("\t视频aid:%s" % i, end="")
        # print("\t视频cid:%s" % video_cid)
    return Video_CID

if __name__ == '__main__':
    Video_AID = get_json('2026561407')
    Video_Cid = get_cid(Video_AID)
    # print(Video_Cid)

文章可自行全篇转载或部分摘取,注明出处→贴个原文链接意思意思一下即可。谢谢😜