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处结束。
我有什么东西变形了吗?
那些列表页应该只有。。。
-
)
-
https://www.beet.tv/author/randrews/page/2
-
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;
}