Home > front end >  Node. Js crawl site image error
Node. Js crawl site image error

Time:12-02

The code is as follows:
//config. Js file code is as follows:
Const url='http://www.dashangu.com/postimg_15672283_4.html';
Const path=the require (" path ");
Const imgDir=path. Join (__dirname, '../img);

The module. Exports. Url=url;
The module. The export. ImgDir=imgDir;
//analyze the js file code is as follows:
Const cheerio=the require (' cheerio ')
The function findImg (dom, callback) {
Let $=cheerio. Load (dom);
$(" img "). Each (function (I, elem) {
Let imgSrc=https://bbs.csdn.net/topics/$(this). Attr (" SRC ");
The callback (imgSrc, I);
})
}
The module. Exports. Findimg=findimg;
//index. Js file code is as follows:
Const request=the require (" request ");
Const path=the require (" path ");
Const fs=the require (" fs ");
Const config=the require ('./config);
Const analyze the require of=('./analyze ');
The function start () {
Request (config url, function (err, res, body) {
The console. The log (" start ");
if(! Err & amp; & Res) {
The console. The log (" start ");
Analyze. FindImg (body, donwLoad);
}
})
}
The function donwLoad (imgUrl, I) {
Let ext=imgUrl. Split ('. '). The pop ();
Request (imgUrl). Pipe (fs) createWriteStream (path. Join (config imgDir, I + ', '+ ext), {
'encoding' : 'utf8'
}))
console.log(i);
}
Start ().



When running index. Js:
Always set a property imgDir of undefined,

CodePudding user response:

Are you breakpoint print: no incoming or across the scope
  • Related