0
일부 콘텐츠에 대해 웹 사이트를 스크랩하려고합니다. 모두 잘 작동하지만 스크랩 된 텍스트를 콘솔에서 사용할 수 있지만 브라우저에서이 스크랩 된 데이터를 인쇄하려고합니다. 콜백을 처리하는 방식에 뭔가 잘못하고 있다고 생각합니다. 아무도 도와 줄 수 있습니까?nodejs 웹 스크래핑 및 콜백 문제
내 코드는 다음과 같습니다 : 데이터를 사용할 수 콜백 함수에서
app.get('/test', function(req, res) {
//All the web scraping magic will happen here
var url = 'https://www.mywebsite.com/path/to/abc';
var allText;
var getTheText = function() {
request(url, function getText(error, response, html){
// First we'll check to make sure no errors occurred when making the request
if(!error){
// Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality
var $ = cheerio.load(html);
// Finally, we'll define the variables we're going to capture
var allText = $('body').children().find('p').text()
console.log('allText');
console.log(allText);
return allText;
}
else {
}
//return result;
});
console.log(allText);
}
getTheText();
console.log('gettheText is ' + getTheText());
res.send(allText);
})
치어 와일 처리 요청을 처리하지 마십시오. redis 또는 kue를 사용하여 백그라운드 작업으로 밀어 넣습니다. 스크래핑을 끝내면 결과를 websocket에 푸시하거나 결과를 가져 오기 위해 ws를 통해 이벤트를 보냅니다. – georoot