Two third-party plug-ins are referenced

  1. Superagent a lightweight, flexible client request proxy module
  2. Cheerio nodejs jq

Simple code

const superagent = require('superagent')
const cheerio = require("cheerio")
const fs = require("fs")

superagent.get('').then(res => {
  const $ = cheerio.load(res.text)
  let el = $('body').find('img'), i) => {
    let img_src = $(i).attr('data-original') || $(i).attr('src'),
      news_title = $(i).attr('alt') | |' '
    if(! img_src)return
    superagent.get(img_src).pipe(fs.createWriteStream('./image/' + index + '.jpg')); })},function (error) {
Copy the code

Train of thought

Using superagent to visit the website, Cheerio parses the page and gets what we want

Then use superagent to fetch the file stream and finally write it to the file


Some sites have restrictions, you can try it yourself

A simple defense is to add headers and so on through the text set method…