用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();
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python爬虫可以用来自动取百度图片,实现批量下载图片的功能。通过使用Python编程语言和相关的库函数,我们可以编写爬虫脚本来完成这个任务。 Python在爬虫领域非常常用,因为它有着丰富的库函数和强大的功能,特别是在人工智能和图像处理方面有着突出的表现。 在取百度图片时,我们需要注意到百度图片的翻页方式是瀑布流,通过滚动滑轮触发JS进行异步加载,而不是之前的点击下一页的方式。这给爬虫增加了一定的难度。但是通过之前的文章,我们可以找到一篇描述了取百度图片的方法,其中介绍的方式可能还是之前的点击下一页的方式。 因此,如果你对爬虫有兴趣,可以尝试使用Python编写一个取百度图片的脚本,通过学习和实践,你可以更深入地了解爬虫的知识和应用。同时,你也可以不定期关注爬虫实例和教程,以获得更多的学习和交流的机会。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python 爬虫(1)--取百度图片,你确定不来看一看](https://blog.csdn.net/weixin_46897073/article/details/111827736)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值