如何将 Apify 网络爬虫范围限制为前三个列表页面?

How to limit Apify web crawler scope to first three list pages?

我在 Apify (jQuery) 中编写了以下网络抓取工具,但我正在努力将其限制为仅查看某些列表页面。

爬虫抓取我在 https://www.beet.tv/author/randrews 上发布的文章,该页面包含 102 个分页索引页,每个索引页包含 20 个文章链接。爬虫在手动完整执行时工作正常;它拥有一切,2,000 多篇文章。

但是,我希望使用 Apify's scheduler 触发偶尔的抓取,只从这些索引 (LIST) 页面的前三个抓取文章(即 60文章)。

调度程序使用 cron 并允许通过输入传递设置 Json。根据建议,我正在使用 "customData"...

{
  "customData": 3
}

...然后下面取该值并用它来限制...

var maxListDepth = parseInt(context.customData); // Jakub's suggestion, Nov 20 2018
if(!maxListDepth || (maxListDepth && pageNumber <= maxListDepth)) {
    context.enqueuePage({

这应该允许脚本在通过调度程序执行时限制范围,但在手动执行时照常进行并完整获取所有内容。

但是,尽管调度程序成功触发了爬虫 - 爬虫仍然会再次运行整个集合;它不会达到 /page/3.

的上限

如何确保我只获得前三页最多 /page/3?

我的格式有问题吗?

在代码中,您可以看到,我以前版本的上述添加现在已被注释掉。


那些 LIST 页面应该只...

  1. 开始的,带有隐含的“/page/1”URL (https://www.beet.tv/author/randrews)
  2. https://www.beet.tv/author/randrews/page/2
  3. https://www.beet.tv/author/randrews/page/3

...而不是 /page/101 或 /page/102 之类的东西,它们可能会浮出水面。


这是关键术语...

START https://www.beet.tv/author/randrews
LIST https://www.beet.tv/author/randrews/page/[\d+]
DETAIL https://www.beet.tv/*
Clickable elements a.page-numbers

这是爬虫脚本...

function pageFunction(context) {

 // Called on every page the crawler visits, use it to extract data from it
 var $ = context.jQuery;

 // If page is START or a LIST,
 if (context.request.label === 'START' || context.request.label === 'LIST') {

     context.skipOutput();

     // First, gather LIST page
     $('a.page-numbers').each(function() {
         // lines added to accept number of pages via customData in Scheduler...
         var pageNumber = parseInt($(this).text());
         // var maxListDepth = context.customData;
         var maxListDepth = parseInt(context.customData); // Jakub's suggestion, Nov 20 2018
         if(!maxListDepth || (maxListDepth && pageNumber <= maxListDepth)) {
           context.enqueuePage({
               url: /*window.location.origin +*/ $(this).attr('href'),
               label: 'LIST'
           });
         }
     });

     // Then, gather every DETAIL page
     $('h3>a').each(function(){
         context.enqueuePage({
             url: /*window.location.origin +*/ $(this).attr('href'),
             label: 'DETAIL'
         });
     });

 // If page is actually a DETAIL target page
 } else if (context.request.label === 'DETAIL') {

     /* context.skipLinks(); */

     var categories = [];
     $('span.cat-links a').each( function() {
         categories.push($(this).text());    
     });
     var tags = [];
     $('span.tags-links a').each( function() {
         tags.push($(this).text());    
     });

     result = {
         "title": $('h1').text(),
         "entry": $('div.entry-content').html().trim(),
         "datestamp": $('time').attr('datetime'),
         "photo": $('meta[name="twitter:image"]').attr("content"),
         categories: categories,
         tags: tags
     };

 }
 return result;
 }

高级设置中有两个选项可以提供帮助:每次抓取的最大页面数和最大结果记录数。在你的情况下,我会将最大结果记录设置为 60,然后爬虫在输出 60 页(来自前 3 个列表)后停止