美文网首页
基于cheerio爬图片简单的demo

基于cheerio爬图片简单的demo

作者: 肖炎_45f8 | 来源:发表于2018-01-03 10:34 被阅读0次

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

相关文章

网友评论

      本文标题:基于cheerio爬图片简单的demo

      本文链接:https://www.haomeiwen.com/subject/htaduxtx.html