【问题标题】:How to pass data between pageFunction executions in Apify web如何在 Apify web 中的 pageFunction 执行之间传递数据
【发布时间】:2019-12-08 04:36:36
【问题描述】:

我正在使用 Apify 抓取网站。我想抓取不同类型的页面,然后将数据组合成一个数据集。现在我为每种页面(用户,镜头)提供了不同的数据集。如何在 pageFunction 执行之间传输数据,例如。计算每个镜头作者的追随者数量。

async function pageFunction(context) {
    const { request, log, jQuery } = context;
    const $ = jQuery;
      
    if (request.url.indexOf('/shots/') > 0) {  
        const title = $('.shot-title').text();
        return {
            url: request.url,
            title
        };
    } else if (request.userData.label === "USER") {
        var followers_count = $('.followers .count').first().text();
        return {
            url: request.url,
            followers_count
        };
    }
}

【问题讨论】:

    标签: javascript web-crawler apify


    【解决方案1】:

    如果我对问题的理解正确,您可以通过爬取的页面传递数据,最后只保存一项。对于这个用例,您可以使用userData,您可以将其传递给每个请求。

    例如,如果您想将数据从/shots 站点传递到USER,您可以这样做。 (但它需要您手动将页面排入队列以控制数据流,这种方法也是这种方法,除了页面的 /shots 类型是您访问的第一个然后继续)

    async function pageFunction(context) {
        const { request, log, jQuery } = context;
        const $ = jQuery;
    
        if (request.url.indexOf('/shots/') > 0) {  
            const title = $('.shot-title').text();
    
            const userLink = 'some valid url to user page'
            //add to the queue your request with the title in the userData
            await context.enqueueRequest({
                url: userLink,
                userData:{
                    label:'USER',
                    shotsTitle: title
                }
            })
    
        } else if (request.userData.label === "USER") {
            var followers_count = $('.followers .count').first().text();
            //here you need to get the shotsTitle and return it
            return {
                url: request.url,
                followers_count,
                shotsTitle: request.userData.shotsTitle
            };
        }
    }
    

    如果您需要分享演员之间的表演,那是另一个话题,如果有帮助,请告诉我。

    还建议您阅读入门指南here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 2012-11-20
      • 1970-01-01
      • 2022-07-29
      相关资源
      最近更新 更多