【问题标题】:How can i achieve multiple page scraping with axios and cheerio如何使用 axios 和 Cheerio 实现多页抓取
【发布时间】:2020-01-31 15:02:22
【问题描述】:

您好,我正在使用 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 => {

【问题讨论】:

    标签: node.js axios cheerio scrape


    【解决方案1】:

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

    如果您知道要抓取多少页

    您可以创建一个简单的for 循环,并将所有axios 函数与生成的网址一一推送到一个数组中。然后你可以用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 分派请求并有条件地迭代。通过这种方式,当您与下面的解决方案进行比较时,您还可以减少内存的最大使用量。而且会比较慢,因为请求不会是并行的。

    // 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;
         }
    
    }
    

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

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

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

    【讨论】:

      猜你喜欢
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多