美文网首页
爬虫-抓取图片

爬虫-抓取图片

作者: 甲乙丙丁0086 | 来源:发表于2018-05-29 00:01 被阅读0次

const request = require('request')
const cheerio = require('cheerio')
const fs = require('fs')
const path = require('path')
let targetUrl = 'http://............'

request(targetUrl, (err, result, body) => {
  if (err) { throw Error(err) }
  let $ = cheerio.load(body)
  $('img').each( function(i) {
    let imgUrl = $(this).attr('src')
    if (!(imgUrl.includes('https') || imgUrl.includes('http'))) {
      imgUrl = 'http:' + imgUrl
    }
    let pop = imgUrl.split('.').pop()
    request(imgUrl).pipe(fs.createWriteStream( path.join(__dirname, i + '.' + pop), {encoding: 'utf8'}))
  })
})
  • 爬取网页图片 下载到本地目录

相关文章

网友评论

      本文标题:爬虫-抓取图片

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