代码之家  ›  专栏  ›  技术社区  ›  Robert Andrews

如何将Apify web爬虫范围限制在前三个列表页?

  •  0
  • Robert Andrews  · 技术社区  · 6 年前

    Apify

    https://www.beet.tv/author/randrews

    然而,我希望使用 Apify's scheduler 触发一次偶然的爬行 仅从这些索引(列表)页面的前三个页面中删除文章

    调度器使用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处结束。

    我有什么东西变形了吗?


    那些列表页应该只有。。。

    1. )
    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;
     }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Jakub Balada    6 年前

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