用js做爬虫帮朋友爬取图片。

爬虫教程

这个教程通俗易懂。

爬取网址

根据满哥学爬虫

  1. 需要下载axios yarn add axios -s
  2. 需要下载 cheerio yarn add cheerio -s
  3. 需要下载 express
  4. 更改 axios.get() 里面的链接就可以。
  5. 本文代码可以直接复制运行。

整体的逻辑 基于这个页面

  1. 先用apifox 测试了一下接口,拿到了整体也页面的数据。
  2. 然后用cheerio去看也面的分页情况 .pagination 找到下面的a标签。
  3. 迭代获取a标签的内容 然后 存储起来 判断 有没有下一页 递归调用函数。。
  4. up主讲的详细。

const axios = require("axios");

const cheerio = require("cheerio");
const fs = require("fs");
const path = require("path")
// console.log(axios);
const urls = [];
const baseUrl = "https://www.jpmn5.com"
const nextText = "下一页"
let index = 0;
const getCosplay = async () => {
    
    
    console.log(index);
    const body = await axios.get(`https://www.jpmn5.com/Cosplay/Cosplay18126${
      
      index ? "_" + index : ""}.html`).then(async res => res.data);
    const $ = cheerio.load(body)

    const page = $(".pagination").eq(0).find("a");

    const pageArr = page.map(function () {
    
    
        return $(this).text()
    }).toArray()
    if (pageArr.includes(nextText)) {
    
    
        $(".article-content p img").each(function () {
    
    
            urls.push(baseUrl + $(this).attr("src"))
        })
        index++;
        await getCosplay()
    }
    // console.log(urls);
    writeFile(urls)
}

const writeFile = function (urls) {
    
    
    urls.forEach(async url => {
    
    
        console.log(url);
        const buffer = await axios.get(url, {
    
     responseType: "arraybuffer" }).then(res => res.data);
        const ws = fs.createWriteStream(path.join(__dirname, '../cos' + new Date().getTime() + ".jpg"))
        ws.write(buffer)
    });
}
getCosplay()
// console.log();

猜你喜欢

转载自blog.csdn.net/qq_43198727/article/details/127449371