当前位置:网站首页>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
就可以看到文件已经被下载下来了
边栏推荐
- China's earthwork tire market trend report, technical dynamic innovation and market forecast
- [C language] twenty two steps to understand the function stack frame (pressing the stack, passing parameters, returning, bouncing the stack)
- 51 lines of code, self-made TX to MySQL software!
- Crawler series (9): item+pipeline data storage
- csapp shell lab
- Cost accounting [23]
- LeetCode#118. Yanghui triangle
- Market trend report, technical innovation and market forecast of Chinese hospital respiratory humidification equipment
- 区间和------离散化
- Crawler series of learning while tapping (3): URL de duplication strategy and Implementation
猜你喜欢
Your wechat nickname may be betraying you
STM32 learning record: play with keys to control buzzer and led
Leetcode notes - dynamic planning -day6
ucorelab3
LeetCode#62. Different paths
JS --- detailed explanation of JS facing objects (VI)
LeetCode#36. Effective Sudoku
ucore lab 6
D - Function(HDU - 6546)女生赛
How to build a nail robot that can automatically reply
随机推荐
LeetCode#198. raid homes and plunder houses
CSAPP shell lab experiment report
STM32学习记录:LED灯闪烁(寄存器版)
LeetCode#204. Count prime
JS --- BOM details of JS (V)
Cost accounting [24]
CS zero foundation introductory learning record
Perinatal Software Industry Research Report - market status analysis and development prospect forecast
Take you to use wxpy to create your own chat robot (plus wechat interface basic data visualization)
ucore Lab 1 系统软件启动过程
STM32学习记录:输入捕获应用
STM32學習記錄:輸入捕獲應用
ucore lab 6
ucore lab5
区间和------离散化
学习记录:理解 SysTick系统定时器,编写延时函数
Leetcode notes - dynamic planning -day7
Brief introduction to libevent
VS2019初步使用
China's salt water membrane market trend report, technological innovation and market forecast