node 爬虫 --- bilibili 视频
一:上一节说到用node 爬虫网络图片
二:这一节 用node 爬 B 站 视频
1. 用到的模块 ( https , ibili )
2. 示例地址 ( https://www.bilibili.com/video/BV1FV411S7MV?p=3)
3.代码
const ibili = require('ibili'); // 下载B站 模块 let https = require('https'); const fs = require('fs') //没有用到 忽略不计 let url = 'https://api.bilibili.com/x/player/pagelist?bvid=BV1QE411s7hT&jsonp=jsonp'; // 网络请求,返回的json 数据 let item_url = 'https://www.bilibili.com/video/BV1FV411S7MV?p='; // json item_url 每个url的地址路劲,参数 ?p = i function video (url, item_url) { let app = https.request(url, res => { let chunks = []; res.on('data', chunk => { chunks.push(chunk) }) res.on('end', () => { let html = Buffer.concat(chunks).toString('utf-8'); // 请求返回的json数据 let arr = JSON.parse(html).data; // 取到 res.data 存为数组 for (let i = 10; i < arr.length; i++) { // arr 遍历 promise(item_url + i, i) // 异步请求 } async function promise (itemUrl, i) { // promise let result = await ibili.downloadVideo({ // ibili.downloadVideo 查看官网 ( https://www.npmjs.com/package/ibili ) folder: 'list', url: itemUrl, filename: i }) console.log(result) } }) }) app.end(); } video(url, item_url)