当前位置:网站首页>nodejs爬虫
nodejs爬虫
2022-07-06 09:25:00 【星时代曹波涛】
nodejs也可以玩爬虫
需要通过npm下在两个模块。
npm init -y
npm install -S axios cheerio
创建index.js文件
const cheerio = require("cheerio")
const axios = require("axios")
const fs = require("fs")
if (!fs.existsSync("download")) {
fs.mkdirSync("download", 777)
}
console.log("开始爬取")
axios.get("http://www.scsoul.top/list/1/20").then(r => {
const $ = cheerio.load(r.data)
$("img").each((index, item) => {
if (index != 0) {
var url = $(item).attr("src");
console.log("开始下载" + url)
axios.get(url, {
responseType: 'stream' }).then(resp => {
let writer = fs.createWriteStream("./download/" + index + ".png");
resp.data.pipe(writer);
console.log(index+".png下载完成")
})
}
})
})
cmd输入
node index.js
就可以看到文件已经被下载下来了
边栏推荐
- LeetCode#198. raid homes and plunder houses
- Cost accounting [17]
- 0 - 1 problème de sac à dos (1)
- LeetCode#53. Maximum subarray sum
- JS --- BOM details of JS (V)
- STM32 learning record: play with keys to control buzzer and led
- JS --- detailed explanation of JS facing objects (VI)
- Unpleasant error typeerror: cannot perform 'ROR_‘ with a dtyped [float64] array and scalar of type [bool]
- Stm32 dossiers d'apprentissage: saisie des applications
- Medical colposcope Industry Research Report - market status analysis and development prospect forecast
猜你喜欢

12306: mom, don't worry about me getting the ticket any more (1)

Lab 8 file system

Want to change jobs? Do you know the seven skills you need to master in the interview software test

程序员的你,有哪些炫技的代码写法?

Brief introduction to libevent

51 lines of code, self-made TX to MySQL software!

毕业才知道IT专业大学生毕业前必做的1010件事

学习记录:STM32F103 时钟系统概述工作原理

STM32 learning record: input capture application

Optimization method of path problem before dynamic planning
随机推荐
0 - 1 problème de sac à dos (1)
China's salt water membrane market trend report, technological innovation and market forecast
动态规划前路径问题优化方式
LeetCode#237. Delete nodes in the linked list
Winter vacation daily question - maximum number of balloons
ucore lab7
Cost accounting [13]
Research Report on medical anesthesia machine industry - market status analysis and development prospect prediction
FSM和i2c实验报告
Cost accounting [14]
FSM and I2C experiment report
LeetCode#2062. Count vowel substrings in strings
CS zero foundation introductory learning record
JS --- detailed explanation of JS facing objects (VI)
Matlab example: two expressions of step function
STM32学习记录:LED灯闪烁(寄存器版)
China chart recorder market trend report, technology dynamic innovation and market forecast
Cost accounting [13]
C语言必背代码大全
LeetCode#118. Yanghui triangle