Web爬虫分页

时间:2020-05-20 14:22:11

标签: pagination apify

我创建了一个Web抓取工具,我试图在该抓取工具中获取加载页面后加载到div中的动态数据。

这是我的代码和来源网站的网址https://www.medizinerkarriere.de/kliniken-sortiert-nach-name.html

async function pageFunction(context) {
    // jQuery is handy for finding DOM elements and extracting data from them.
    // To use it, make sure to enable the "Inject jQuery" option.
    const $ = context.jQuery;
    var result = [];
    $('#klinikListBox ul').each(function(){        
        var item = {           
            Name: $(this).find('li.klName').text().trim(),
            Ort: $(this).find('li.klOrt').text().trim(),
            Land: $(this).find('li.klLand').text().trim(),            
            Url:""
        };
        result.push(item);    
    });

    // To make this work, make sure the "Use request queue" option is enabled.
    await context.enqueueRequest({ url: 'https://www.medizinerkarriere.de/kliniken-sortiert-nach-name.html' });

    // Return an object with the data extracted from the page.
    // It will be stored to the resulting dataset.
    return result;
}

但是有点击分页,我不确定该怎么做。

我尝试了此链接中的所有方法,但是没有用。

https://docs.apify.com/scraping/web-scraper#bonus-making-your-code-neater

非常感谢您的帮助和快速帮助。

1 个答案:

答案 0 :(得分:1)

在这种情况下,分页会动态加载到单个页面上,因此使新页面入队是没有意义的。您只需单击页面按钮即可进入下一页,这也是一种好的做法,在单击后稍等片刻。

$('#PGPAGES span').eq(1).click();
await context.waitFor(1000)

您可以通过简单的循环抓取所有页面

const numberOfPages = 8 // You can scrape this number too
for (let i = 1; i <= numberOfPages; i++) {
    // Your scraping code, push data to an array and return them in the end
    $('#PGPAGES span').eq(i).click();
    await context.waitFor(1000)
}