Nodejs实现简单的网页图片爬取

279 阅读1分钟

使用Nodejs内置模块 + shell脚本 实现简单的图片爬虫

​ 用到的模块 https 用于请求child_process 用于执行shell脚本

引入内置模块 http / https

const http = require('http');

引入spawn模块 用于执行shell脚本

const { spawn } = require('child_process');

定义要获取图片的url地址

const url = 'https://www.duitang.com/article/?id=870171';

定义正则匹配

const reg = /http(s)?:\/\/([\S]*)\.(jpg|jpeg|png|gif|webp)/g;

发起请求

// 发起请求
https.get(url, res => {

	// 定义空字符串
	let str = '';

	// 监听加载
	res.on('data', data => {
		str += data;
	})

	// 监听结束
	res.on('end', () => {
		
        let i;
        
		// 每次匹配一个
		while (i = reg.exec(str)) {

			// 取每条数据
			let item = i[0];

			// 执行shell脚本 传递参数 参数为 img的 地址链接
			spawn('sh', ['index.sh', item]);

		}
		console.log('执行')
	})
})

shell脚本编写

#!/bin/sh

# 定义命名 时间戳 + 随机数
time=$(date "+%Y%m%d%H%M%S")-$RANDOM

# 取图片格式
type="${1##*.}"

# 如果不存在此目录 则创建
if [ ! -d "img" ]; then
    mkdir img
fi

# 发起请求 输出到指定目录指定文件名
curl $1 -o ./img/$time.$type

# 退出脚本
exit 0;

执行index.js脚本

node index.js 

执行脚本一定要在当前目录下。

如果url 是https 开头 那么发起请求 就需要用https 否则http就可以。

正常执行后,会在当前目录下,新建img文件夹,里面是下载好的图片。

index.js 完整代码

index.sh 完整代码