我想抓取youtube频道,我列出了1500个频道列表。我想通过节点一一刮掉它。 问题出在循环中,我想停止循环,直到获得当前通道的响应和处理响应为止。
异步并等待
const cheerio = require('cheerio');
const request = require('request');
var fs = require('fs');
async function getTitle(){
var data = await fs.readFileSync('./channels.json');
var Parsed_Data = await JSON.parse(data);
console.log(Parsed_Data);
for (x of Parsed_Data){
// var url = 'https://www.youtube.com/channel/' + x.id + '/about';
// console.log(url);
request( 'https://www.youtube.com/channel/' + x.id + '/about', function(err, res, body) {
let $ = cheerio.load(body);
console.log($);
let title = $('title');
console.log(title.text());
}
)};
想要每个频道的标题
答案 0 :(得分:0)
您可以这样做:
function getBody(url) {
return new Promise((resolve, reject) => {
request( 'https://www.youtube.com/channel/' + x.id + '/about', function(err, res, body) {
if (err) {
return reject(err);
}
resolve(body);
});
})
}
async function getTitle(){
var data = await fs.readFileSync('./channels.json');
var Parsed_Data = await JSON.parse(data);
console.log(Parsed_Data);
for (x of Parsed_Data){
const body = await getBody('https://www.youtube.com/channel/' + x.id + '/about');
let $ = cheerio.load(body);
console.log($);
let title = $('title');
console.log(title.text());
)};
答案 1 :(得分:0)
首先,安装请求承诺:
npm i request-promise
然后,将代码更改为此:
const cheerio = require('cheerio');
const rp = require('request-promise');
var fs = require('fs');
async function getTitle(){
var data = await fs.readFileSync('./channels.json');
var Parsed_Data = JSON.parse(data);
console.log(Parsed_Data);
for (x of Parsed_Data){
// var url = 'https://www.youtube.com/channel/' + x.id + '/about';
// console.log(url);
let body=await rp( 'https://www.youtube.com/channel/' + x.id + '/about',
let $ = cheerio.load(body);
console.log($);
let title = $('title');
console.log(title.text());
}
}
实际上,您不需要在JSON.parse(data);旁边等待。