node.js - 我如何使用axios和cheerio实现多页面抓取

标签 node.js axios cheerio scrape

您好,我正在使用 axios 和 Cheerio 来抓取一些数据。我想抓取多个页面, url 结构类似于 example.com/?page=1。我如何用计数器抓取每个页面?

axios({
    method: "get",
    url:
      "https://example.com/?page=",
    headers: {
      "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
    }
  }).then(res => {

最佳答案

我相信有多种方法可以实现该解决方案,但基本上您需要执行所有 axios 并以编程方式使用 Cheerio 解析所有它们。

如果您知道要抓取多少页

您可以创建一个简单的 for 循环,并将所有 axios 函数与生成的 url 一起推送到一个数组中。然后你可以使用 Promise.all

调用它们
const promises = [];

for(let page = 0; page <= 5; page ++){
     promises.push(
          axios({method: "get",url:`https://example.com?page=${page}`})
          .then(res => {
              // Parse your result with Cheerio or whatever you like
          })
     );
}

// You can pass the responses on this resolve if you want.
Promise.all(promises).then(...)

如果您正在抓取列表页面并且总页数未知

然后,您可以创建一个异步/递归函数,用于使用 axios 分派(dispatch)请求并有条件地迭代。通过这种方式,与下面的解决方案相比,您还可以减少内存的最大使用量。而且它会更慢,因为请求不会并行。

// The function below is kind-of pseudo code so don't try to copy/paste it :) 
const dispatchRequest = (page) => {
     const response = axios({url: `https://example.com?page=${page}`});
     // Ex: You can parse the response here with Cheerio and check if pagination is not disable
     if(something){
          return dispatchRequest(page+1);
     }
     else{
         return response;
     }

}

上述解决方案当然也有缺点。如果您被目标网站阻止或您的请求因某种原因失败,您将没有机会重试相同的请求或轮换代理以绕过目标网站的安全性。

我建议您实现一个队列并将所有请求分派(dispatch)函数放在那里。通过这种方式,您可以检测失败/问题并再次将失败的请求排队。您还可以通过队列支持来实现上述两种解决方案。您可以并行运行它并更好地管理内存/CPU 消耗。

您也可以使用 SDK。我看到有几个抓取 SDK 为您提供了整个工具集,因此您不必重新发明轮子。

关于node.js - 我如何使用axios和cheerio实现多页面抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60006377/

相关文章:

node.js - 如何启动 Node 服务器并导出 NODE_PATH(使用 npm start 或内联命令)

node.js - NodeJS 将二进制数据写入文件而不覆盖

vue.js - 无法使用 Vue 和 Axios 查询 AWS API

python - 属性错误: 'NoneType' object has no attribute 'save'

JavaScript - 根据一定的逻辑命名对象属性

javascript - 使用 jQuery/cheerio 访问脚本标签中的变量

javascript - NodeJS 警告 : possible event emitter leak. 11 添加了开放监听器

node.js - 将 null 参数传递给 v8 回调

javascript - 如何使用 Axios reactjs 获取 303 请求的响应头

javascript - cheerio/jquery 选择器 : how to get a list of elements in nested div's?