const Promise = require('bluebird')
const requestPro = Promise.promisify(request)
const cheerio = require('cheerio')
let pageUrl ='https://www.jianshu.com/'
async function crawPics(url, dirname) {
let html = await requestPro({ url }).then(response => response.body)
$ = cheerio.load(html)
// let imgUrls = $('a.collection img').attr('src')//也不知为啥这句话不能用
let imgUrls=Array.from($('a.collection').find('img')).map((element) =>element.attribs.src.split("?").shift())
console.log(imgUrls)
imgUrls.forEach(async (imgUrl,index) => {
console.log(imgUrl)
console.log(`${dirname}\\cup${index}.jpg`)
request('http:'+imgUrl).pipe(fs.createWriteStream( `${dirname}\\cup${index}.jpg`))
});
}
crawPics(pageUrl,path.join(__dirname, 'cup'))
但是头疼的是不知$('a.collection img').attr('src')为啥不能用了,到底attr与attribs怎么区分啊,还是cheerio已经更新了。不过这个demo我测试了 ,能用。也比较简洁了。
da