Я написал скрипт на node
, чтобы вычистить titles
и urls
для заголовков различных сообщений со целевой страницы веб-страницы, а затем извлечь profile name
каждого пользователя с его внутренней страницы. , Единственное, что я не могу решить, это напечатать две вещи items
и output
внутри for loop
.
Как я могу передать items
в for loop
, чтобы скрипт мог одновременно печатать items
и output
?
Я пробовал как:
var request = require('request');
var cheerio = require('cheerio');
const url = 'https://stackoverflow.com/questions/tagged/web-scraping';
const host = 'https://stackoverflow.com';
function getPosts() {
request(url, function(error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
let linkstorage = [];
$('.summary .question-hyperlink').each(function() {
var items = $(this).text();
var links = host + $(this).attr("href");
linkstorage.push(links);
});
for (const newlink of linkstorage) {
request(newlink, function(error, response, html) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
var output = $(".user-details a").eq(0).text();
console.log(output);
}
});
}
}
});
}
getPosts();