node-axios爬虫

基本使用

1
const axios = require("axios");

get请求

1
axios.get(url: string, config?: AxiosRequestConfig)
  • url:请求地址
  • config:可选参数,对象类型的。常用参数:
    • responseType:请求类型
    • headers:请求头

下载一张图片

1
2
3
4
5
6
7
8
import axios from "axios";
import fs from "fs";

axios.get("https://gitee.com/lishushuai/img/raw/master/20210318234209.jpg", {
responseType: "stream"
}).then(resp => {
resp.data.pipe(fs.createWriteStream("./img/1.jpg"))
})