最近闲来无事,尝试了一下bilibili的视频爬取。
接下来我们要做的就是构造这两个参数,为了描述方便我们将其称为参数1和参数2
这个参数我们可以通过以下代码构建
int(time.time() * 1000)
Math.random().toString().substr(2);
//运行结果为"7702046386981107"
Math.random()
//运行结果为0.7702046386981107
call_back = 'jqueryCallback_bili_'
e = '%.18f'% random.random()
call_back = call_back + e.split('.')[-1]
6.接下来我们直接对该网址进行请求,我们请求到的结果是这种格式:
7.我们要做的是将其json格式的数据,以方便处理,可以通过以下代码:
resp = requests.get(url,headers = headers)
jsonStr = re.sub('jqueryCallback_bili_.*?\(','',resp.text)
jsonStr = jsonStr[:-1]
#结果为:
#{"code":0,"message":"0","ttl":1,"data":{"archives":[{"aid":455463907,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i0.hdslb.com/bfs/archive/4f0726207b2f24b464aa519ad19d58a504153cc0.jpg","title":"【芒种】白小白老师编舞 新人up主 多多支持下哦","pubdate":1587815012,"ctime":1587815012,"desc":"","state":0,"attribute":16768,"duration":94,"mission_id":13243,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":1,"no_reprint":1,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":166379333,"name":"小钰女子","face":"http://i0.hdslb.com/bfs/face/ca3f439992617f06963586c3cb8414fc265b662f.jpg"},"stat":{"aid":455463907,"view":1,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":0,"dislike":0},"dynamic":"#芒种##爵士舞##古典舞#","cid":183162808,"dimension":{"width":2048,"height":1080,"rotate":0},"bvid":"BV1X5411x75s"},{"aid":412912231,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i1.hdslb.com/bfs/archive/677024d1e45abb5fd630a0fdb132a946e44a9687.jpg","title":"原创《苏幕遮》","pubdate":1587814739,"ctime":1587814739,"desc":"原创不易\n翻调改编请标明出处:花儿艺术","state":0,"attribute":8405248,"duration":114,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":1,"no_reprint":0,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":305133937,"name":"花芊芊舞蹈","face":"http://i0.hdslb.com/bfs/face/23eb29488a432bf4aa2a58a8ec261876eb9d92f4.jpg"},"stat":{"aid":412912231,"view":4,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":0,"dislike":0},"dynamic":"#苏幕遮# #古风舞# #汉服舞蹈#","cid":183147159,"dimension":{"width":1920,"height":1080,"rotate":0},"bvid":"BV1wV411d7pZ"},{"aid":882907467,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i0.hdslb.com/bfs/archive/4f0726207b2f24b464aa519ad19d58a504153cc0.jpg","title":"【芒种】古风舞蹈 白小白老师编舞 新人up主 多多支持","pubdate":1587814630,"ctime":1587814630,"desc":"","state":0,"attribute":16768,"duration":94,"mission_id":13137,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":1,"no_reprint":1,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":166379333,"name":"小钰女子","face":"http://i0.hdslb.com/bfs/face/ca3f439992617f06963586c3cb8414fc265b662f.jpg"},"stat":{"aid":882907467,"view":0,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":0,"dislike":0},"dynamic":"#芒种##爵士舞##现代舞#","cid":183136464,"dimension":{"width":2048,"height":1080,"rotate":0},"bvid":"BV1nK4y1b7w1"},{"aid":882921441,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i0.hdslb.com/bfs/archive/efe1a0ac994acad38a1b12d0be18305ec6cf8266.jpg","title":"《苏慕遮》","pubdate":1587814039,"ctime":1587814039,"desc":"-","state":0,"attribute":8405248,"duration":114,"mission_id":13259,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":1,"no_reprint":0,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":272426938,"name":"Lin--xf","face":"http://i2.hdslb.com/bfs/face/b4917db416d7695b9145aebb721373d4e692f7a8.jpg"},"stat":{"aid":882921441,"view":2,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":0,"dislike":0},"dynamic":"#VLOG的一百种可能# #古典舞# #苏幕遮#","cid":183148843,"dimension":{"width":1920,"height":1080,"rotate":0},"bvid":"BV1VK4y1b72n"},{"aid":242966163,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i2.hdslb.com/bfs/archive/f9645da883246e138a10a5ba8974f8c4250edc16.jpg","title":"翻跳小练习1(cover多情种)","pubdate":1587813836,"ctime":1587813836,"desc":"gregorius.","state":0,"attribute":8405315,"duration":121,"mission_id":13110,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":1,"no_reprint":0,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":549486274,"name":"bili_33886127677","face":"http://i0.hdslb.com/bfs/face/member/noface.jpg"},"stat":{"aid":242966163,"view":3,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":1,"now_rank":0,"his_rank":0,"like":0,"dislike":0},"dynamic":"#学霸修炼挑战# #舞蹈翻跳# #古典舞#","cid":183136708,"dimension":{"width":1080,"height":1920,"rotate":0},"bvid":"BV1ye411s7Kj"},{"aid":285463336,"videos":1,"tid":200,"tname":"中国舞","copyright":2,"pic":"http://i1.hdslb.com/bfs/archive/a1e4803de13d6aefabd4d613918cadb38aa9682a.jpg","title":"中国古典舞 手位 手型 准备位","pubdate":1587813269,"ctime":1587813269,"desc":"爱奇艺","state":0,"attribute":16384,"duration":391,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":0,"no_reprint":0,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":253429180,"name":"丁慧是五妹","face":"http://i0.hdslb.com/bfs/face/3424ebe6ff9e3faa3e6668922a01609406e310ab.jpg"},"stat":{"aid":285463336,"view":1,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":0,"dislike":0},"dynamic":"#古典舞#","cid":183147950,"dimension":{"width":1440,"height":900,"rotate":0},"bvid":"BV11f4y1S72e"},{"aid":455486051,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i1.hdslb.com/bfs/archive/5209466c2be7431d41335f7b1e81c65a86276d53.jpg","title":"【BMS中国舞】古典舞《苏慕遮》舞蹈","pubdate":1587812833,"ctime":1587798853,"desc":"","state":0,"attribute":16512,"duration":143,"mission_id":13243,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":0,"no_reprint":1,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":388154349,"name":"辰威BMS舞蹈贴士君","face":"http://i0.hdslb.com/bfs/face/55569789ee62c07975584f8b7735f2127f96bd9c.jpg"},"stat":{"aid":455486051,"view":14,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":2,"dislike":0},"dynamic":"#bilibili新星计划##古典舞##中国风#","cid":182980548,"dimension":{"width":1280,"height":720,"rotate":0},"bvid":"BV1M541147sZ"},{"aid":242943477,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i0.hdslb.com/bfs/archive/5029b30e1f35d378fb58f14cfc0a83c33e1fc61f.jpg","title":"兰州中山桥飞天舞","pubdate":1587812406,"ctime":1587812406,"desc":"-","state":0,"attribute":16512,"duration":61,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":0,"no_reprint":1,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":256126953,"name":"涼州刺史","face":"http://i1.hdslb.com/bfs/face/c908aad73d6c50027c97cb1ea300e0fcf3a04a4e.jpg"},"stat":{"aid":242943477,"view":3,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":0,"dislike":0},"dynamic":"#跳舞##古典舞##民族舞#","cid":183143831,"dimension":{"width":720,"height":1280,"rotate":0},"bvid":"BV1Ue411s7jv"},{"aid":837768140,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i0.hdslb.com/bfs/archive/4342652b4590bd65bc177fea8594a66173465fa9.jpg","title":"【柳长央】成年作《囡囡》翻跳(/▽\)成功长成170+的壮汉","pubdate":1587808819,"ctime":1587808822,"desc":"创作类型:翻跳\n编舞出处:BV1ob411P73P 金刚舞蹈\n音乐:《the Shanghai》\n简介:想来想去还是这支五十秒《囡囡》最适合拿来做我的成年作,原视频下面解释说是小女孩得到心爱的发饰后的喜悦之情,我也很少会尝试这种舞蹈,非专业请多包涵我的不足。救命我最近胖了好多::\u003e_\u003c::\n直接在楼顶天台录了有点草率对不起!!\n不知不觉就成年啦,但还是和以前一样傻傻的","state":0,"attribute":16768,"duration":57,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":1,"no_reprint":1,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":322218228,"name":"柳长央","face":"http://i2.hdslb.com/bfs/face/72c39152b9ed25b03f8ae03c470233d6908ab74c.jpg"},"stat":{"aid":837768140,"view":10,"danmaku":0,"reply":1,"favorite":1,"coin":2,"share":0,"now_rank":0,"his_rank":0,"like":1,"dislike":0},"dynamic":"#舞蹈##古典舞##翻跳#(本来不想发的因为这支视频我真的又胖又丑 求别取关!╥﹏╥)\n想来想去还是这支50秒《囡囡》最适合拿来做我的成年作,原视频下面解释说是小女孩得到心爱的发饰后的喜悦之情,我也很少会尝试这种舞蹈,非专业请多包涵我的不足。救命我最近胖了好多::\u003e_\u003c::\n直接在楼顶天台录了有点草率对不起!!","cid":179915517,"dimension":{"width":3840,"height":2160,"rotate":0},"bvid":"BV1kg4y187vW"},{"aid":925472317,"videos":1,"tid":200,"tname":"中国舞","copyright":2,"pic":"http://i2.hdslb.com/bfs/archive/11bc7444eb539316bee898f7a47e30073ad499d3.jpg","title":"《琵琶行》小比克/小beek","pubdate":1587807476,"ctime":1587807476,"desc":"小beek微博","state":0,"attribute":8404992,"duration":60,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":0,"no_reprint":0,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":109319037,"name":"柚子也很软","face":"http://i2.hdslb.com/bfs/face/fc9249d452f4acd5671abb504769e8fdb1723059.jpg"},"stat":{"aid":925472317,"view":6,"danmaku":0,"reply":0,"favorite":2,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":2,"dislike":0},"dynamic":"#琵琶行# #古典舞# #古风舞#","cid":183112778,"dimension":{"width":720,"height":1280,"rotate":0},"bvid":"BV1uT4y1374d"},{"aid":752935196,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i2.hdslb.com/bfs/archive/1fc967d2128a0458d721b9a528f90c5ddad4c534.jpg","title":"【舞蹈】苏幕遮 斯斯老师 第一段 表情真好!","pubdate":1587802083,"ctime":1587802083,"desc":"不小心出镜了 尴尬","state":0,"attribute":8405376,"duration":36,"mission_id":13243,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":1,"no_reprint":1,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":19424425,"name":"小孙不跳舞","face":"http://i1.hdslb.com/bfs/face/4bdf2f9eb0c5f9d1177ce1c656e83414aedd9937.jpg"},"stat":{"aid":752935196,"view":10,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":0,"dislike":0},"dynamic":"#bilibili新星计划# #舞蹈# #古典舞#","cid":183070368,"dimension":{"width":1920,"height":1080,"rotate":0},"bvid":"BV1zk4y1r7rU"},{"aid":327882734,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i2.hdslb.com/bfs/archive/c4465daef9692f20ddf83a5ead005367ebcfe060.jpg","title":"《琵琶语》古风爵士舞【Sunshine Dance NYC】老师学生练习合集","pubdate":1587798814,"ctime":1587798814,"desc":"创作类型:翻跳\n编舞:刘婷婷\n舞蹈工作室:Sunshine Dance NYC\n指导老师:Coco Li\n学员舞者:YQ社会黄,熊,郑同学,田澍,Michelle\n歌曲:琵琶语-詹伟杰\n简介:由纽约Sunshine舞团教师及学员出品作业合集,翻跳刘婷婷的原创古风爵士舞《琵琶语》- 今夜闻君琵琶语,如听仙乐耳暂明。莫辞更坐弹一曲,为君翻作琵琶行。","state":0,"attribute":16768,"duration":95,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":1,"no_reprint":1,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":395292245,"name":"Sunshine_Dance","face":"http://i1.hdslb.com/bfs/face/d29683cd0f0f8f81f0e060daf62b9cfaaf76cd4f.jpg"},"stat":{"aid":327882734,"view":4,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":2,"dislike":0},"dynamic":"#古典舞##古风舞##中国舞##Sunshine舞团##SunshineDanceNYC##舞蹈##中国风##翻跳##纽约舞蹈##纽约舞蹈工作室##古风##琵琶语##古风爵士舞##古典舞爵士##作业##作品集##爵士##合集##练习室##舞室##","cid":183039368,"dimension":{"width":1920,"height":1080,"rotate":0},"bvid":"BV1oA411b79e"},{"aid":327959486,"videos":1,"tid":200,"tname":"中国舞","copyright":2,"pic":"http://i2.hdslb.com/bfs/archive/fbcc030c7708777c231ac145dda68862d5838eb7.jpg","title":"最美新版敦煌飞天仙女","pubdate":1587797192,"ctime":1587797192,"desc":"抖音","state":0,"attribute":16451,"duration":19,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":0,"no_reprint":0,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":357161283,"name":"熔鑫","face":"http://i1.hdslb.com/bfs/face/699ec14a3c59a00a05414ac6fbe6f6a96e6a63de.jpg"},"stat":{"aid":327959486,"view":11,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":0,"dislike":0},"dynamic":"#敦煌舞##飞天##仙女#","cid":183036800,"dimension":{"width":544,"height":960,"rotate":0},"bvid":"BV18A411471x"},{"aid":967888145,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i0.hdslb.com/bfs/archive/15578a4e441ed7808759e626d429f357db9cb937.jpg","title":"【墨寒笙】木兰行 ❀愿为市鞍马 从此替爷征❀剑舞","pubdate":1587796081,"ctime":1587796081,"desc":"半原创\n不拿剑的部分,动作取自上海市舞蹈学校 木兰辞 表演王思佳\nbgm 忘川风华录--木兰行 https://www.bilibili.com/video/BV1Gg4y1a7vg?from=search\u0026seid=16407216514664503602","state":0,"attribute":16768,"duration":134,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":1,"no_reprint":1,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":59172820,"name":"墨寒笙","face":"http://i2.hdslb.com/bfs/face/b4d05b22d17cb92e5c8c915c94d4e7b0314d4f3b.jpg"},"stat":{"aid":967888145,"view":614,"danmaku":9,"reply":39,"favorite":45,"coin":45,"share":7,"now_rank":0,"his_rank":0,"like":158,"dislike":0},"dynamic":"#舞蹈##古典舞#","cid":183015195,"dimension":{"width":1920,"height":1080,"rotate":0},"bvid":"BV1Bp4y1X7Gz"},{"aid":327952535,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i2.hdslb.com/bfs/archive/ca3aff89e9ac6c8b3d811e5b48926cb631c9b789.jpg","title":"【唐诗逸】进来白嫖女神!舞蹈混剪《思美人兮》","pubdate":1587795502,"ctime":1587795502,"desc":"-","state":0,"attribute":16512,"duration":268,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":0,"no_reprint":1,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":358295001,"name":"萝卜菜头susu","face":"http://i1.hdslb.com/bfs/face/3372b03f07c6c8fe2ae60bd14b5f2f6ee8ab40e4.jpg"},"stat":{"aid":327952535,"view":21,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":2,"dislike":0},"dynamic":"#舞蹈##女神##唐诗逸#","cid":183025059,"dimension":{"width":1344,"height":720,"rotate":0},"bvid":"BV1qA41147Vg"},{"aid":840427910,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i1.hdslb.com/bfs/archive/d212e519886ad5cdf1f4ad35d210eab94d616ed0.jpg","title":"【鱼丫头】 醉(part 1)","pubdate":1587793680,"ctime":1587793680,"desc":"第一段,先留个底,yeah,顺便,封面无关","state":0,"attribute":16512,"duration":41,"mission_id":13106,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":0,"no_reprint":1,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":274667,"name":"白色闪光","face":"http://i2.hdslb.com/bfs/face/0b242dab260122fb8813e1c9ba48e20632db0bb5.jpg"},"stat":{"aid":840427910,"view":14,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":1,"now_rank":0,"his_rank":0,"like":1,"dislike":0},"dynamic":"#全能打卡挑战##古典舞##试跳#","cid":183003629,"dimension":{"width":1080,"height":1920,"rotate":0},"bvid":"BV1u54y1R7mB"},{"aid":455393668,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i0.hdslb.com/bfs/archive/2dbb50969939995312c8597aed8556022cf0fb5c.jpg","title":"兜兜的第一个投稿","pubdate":1587793396,"ctime":1587793396,"desc":"这是我的第一个投稿请大家多多支持,如果有意见的话可以提出来","state":0,"attribute":8405120,"duration":182,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":0,"no_reprint":1,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":455142906,"name":"糯米小豆豆","face":"http://i2.hdslb.com/bfs/face/61a66863f61408ba5c141d1534d57b0aac78b6b5.jpg"},"stat":{"aid":455393668,"view":2,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":0,"dislike":0},"dynamic":"#舞蹈# #现场# #古典舞#","cid":183003098,"dimension":{"width":360,"height":640,"rotate":0},"bvid":"BV1h5411x7Wk"},{"aid":837944551,"videos":1,"tid":200,"tname":"中国舞","copyright":2,"pic":"http://i2.hdslb.com/bfs/archive/d9be55a4623b5d280ae1855d773b931460941192.jpg","title":"基本舞姿","pubdate":1587792373,"ctime":1587792373,"desc":"舞蹈老师","state":0,"attribute":16384,"duration":57,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":0,"no_reprint":0,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":301806338,"name":"黑恩z","face":"http://i0.hdslb.com/bfs/face/ea979cf6960099cc75ac5d4a0e1b5021fc40cc0f.jpg"},"stat":{"aid":837944551,"view":5,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":0,"dislike":0},"dynamic":"#古典舞#","cid":183008777,"dimension":{"width":352,"height":288,"rotate":0},"bvid":"BV1Sg4y1z7CM"},{"aid":582896681,"videos":1,"tid":200,"tname":"中国舞","copyright":1,"pic":"http://i0.hdslb.com/bfs/archive/797507f48d270dce569d6ad74ff4275039c043f4.jpg","title":"【丁慧】原得一心人 周深 舞蹈","pubdate":1587790649,"ctime":1587790649,"desc":"","state":0,"attribute":16512,"duration":32,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":0,"no_reprint":1,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":253429180,"name":"丁慧是五妹","face":"http://i0.hdslb.com/bfs/face/3424ebe6ff9e3faa3e6668922a01609406e310ab.jpg"},"stat":{"aid":582896681,"view":14,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":1,"dislike":0},"dynamic":"#舞蹈##古典舞##周深#","cid":183117022,"dimension":{"width":960,"height":540,"rotate":0},"bvid":"BV1Z64y1T7r4"},{"aid":327881796,"videos":1,"tid":200,"tname":"中国舞","copyright":2,"pic":"http://i1.hdslb.com/bfs/archive/ac5b104afc447983f79330e675dbc7eeddec515e.jpg","title":"横拧","pubdate":1587790039,"ctime":1587790039,"desc":"舞蹈老师","state":0,"attribute":16384,"duration":74,"rights":{"bp":0,"elec":0,"download":0,"movie":0,"pay":0,"hd5":0,"no_reprint":0,"autoplay":1,"ugc_pay":0,"is_cooperation":0,"ugc_pay_preview":0,"no_background":0},"owner":{"mid":301806338,"name":"黑恩z","face":"http://i0.hdslb.com/bfs/face/ea979cf6960099cc75ac5d4a0e1b5021fc40cc0f.jpg"},"stat":{"aid":327881796,"view":1,"danmaku":0,"reply":0,"favorite":0,"coin":0,"share":0,"now_rank":0,"his_rank":0,"like":0,"dislike":0},"dynamic":"#古典舞#","cid":182993402,"dimension":{"width":960,"height":720,"rotate":0},"bvid":"BV1oA411b7ku"}],"page":{"count":13496,"num":1,"size":20}}}
8.接下来我们要做得失构造网址,即详情播放页面的网址,如:https://www.bilibili.com/video/BV1X5411x75s这种格式
jsons = json.loads(jsonStr)
results = jsons["result"]
print(results)
for result in results:
href = 'https://www.bilibili.com/video/{}'.format(result['bvid'])
headers = {
'Referer': 'https://www.bilibili.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
}
url = 'https://www.bilibili.com/video/BV1X5411x75s'
resp = requests.get(url, headers=headers)
没错这也是json数据,那就好办了,我们这里主要用的是该json数据中的audio–音频,video–视频,获得其相应的请求网址,其中我们以获取其baseUrl为例代码如下:
jsonStr = resp.text.split('window.__playinfo__=')[-1].split('<')[0].split(';')[0]
jsons = json.loads(jsonStr)
data = jsons["data"]
mp3_path = ''
mp4_path = ''
#当音频视频分开的时候
try:
dash = data['dash']
videos = dash['video']
audios = dash['audio']
mp3_path = audios[0]['baseUrl']
mp4_path = videos[0]['baseUrl']
#音频视频没有分开的时候
except:
mp4_path = ''
durls = data['durl']
for durl in durls:
if durl['order'] == 1:
mp4_path = durl['url']
print(mp3_path)
print(mp4_path)
13.我们可以通过以下代码来下载视频和音频
import requests
import get_mp3_mp4_path as ph
def download_mp4(min,max,url,title):
if url == '':
return ''
headers = {
'Range': 'bytes={}-{}'.format(min,max),
'Referer': 'https://www.bilibili.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
}
resp = requests.get(url, headers=headers)
if resp.status_code != 206:
return resp.status_code
# print(resp.text)
print(resp.status_code)
with open('videos/{}.mp4'.format(title), 'ab+') as f:
f.write(resp.content)
return 0
def download_mp3(min,max,url,title):
# 如果没有音频
if url == '':
return ''
headers = {
'Range': 'bytes={}-{}'.format(min,max),
'Referer': 'https://www.bilibili.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
}
resp = requests.get(url,headers = headers)
if resp.status_code != 206:
return resp.status_code
print(resp.status_code)
# print(resp.text)
with open('audios/{}.mp3'.format(title),'ab+') as f:
f.write(resp.content)
return 0
def download_mp3_mp4(mp3_path, mp4_path,title):
min = 0
max = 200000
mp4_status_code = 0
mp3_status_code = 0
while True:
if mp4_status_code == 0:
mp4_status_code = download_mp4(min, max,mp4_path,title)
print("mp4_status_code:",mp4_status_code)
else:
pass
if mp3_status_code == 0:
mp3_status_code = download_mp3(min, max,mp3_path,title)
print('mp3_status_code:',mp3_status_code)
else:
pass
if mp4_status_code != 0 and mp3_status_code != 0:
break
min = max + 1
max = max + 200000
def main():
url = 'https://www.bilibili.com/video/BV1X5411x75s'
mp3_path, mp4_path = ph.get_mp3_and_mp4_path(url)
print(mp4_path)
title = 'test'
download_mp3_mp4(mp3_path, mp4_path, title)
if __name__ == '__main__':
main()
import subprocess
#mp4_path:mp4视频的存放路径
#mp3_path:mp3视频的存放路径
# output_path:合成视频的存放路径
try:
subprocess.call('ffmpeg -i ' + mp4_path + ' -i ' + mp3_path + ' -vcodec copy -acodec copy ' + output_path, shell=True)
except:
pass
注意:在这一步时你的电脑需安装ffmpeg,这个的安装就不再描述了,麻烦自行百度。
15.以上步骤完成后我们就可以欣赏我们下好的小视频了:
16.免责声明:该爬虫仅限于学习之用,若用于商用或其他,导致的后果请自行承担,概不负责。