Javascript 从CasperJS中的无限滚动条中动态地抓取呈现的链接
我正试图在使用CasperJS的左侧边栏上刮取链接 侧边栏中有数百个链接,但向下滚动时一次只加载20个链接。此代码成功捕获了前20个(需要全局安装casperjs和phantomjs才能运行): 我可以使用以下命令在实际浏览器中向下滚动页面:Javascript 从CasperJS中的无限滚动条中动态地抓取呈现的链接,javascript,jquery,web-scraping,casperjs,infinite-scroll,Javascript,Jquery,Web Scraping,Casperjs,Infinite Scroll,我正试图在使用CasperJS的左侧边栏上刮取链接 侧边栏中有数百个链接,但向下滚动时一次只加载20个链接。此代码成功捕获了前20个(需要全局安装casperjs和phantomjs才能运行): 我可以使用以下命令在实际浏览器中向下滚动页面: $('#search-left-inner').scrollTop(10000); 10000是一个任意大的数字;每次在浏览器中运行该代码时,它都会加载20多个链接。(理想情况下,我希望能够一次抓取所有内容,而不必一次重新加载20个,但现在这就不那么紧迫
$('#search-left-inner').scrollTop(10000);
10000是一个任意大的数字;每次在浏览器中运行该代码时,它都会加载20多个链接。(理想情况下,我希望能够一次抓取所有内容,而不必一次重新加载20个,但现在这就不那么紧迫了。)
如果我将该行放在getAllLinks
函数中,如下所示:
var getAllLinks = function() {
$('#search-left-inner').scrollTop(10000);
var linksOnThisPage = []
//etc, etc,
它仍然只加载20个链接。许多类似的帖子都讨论了同步性问题,所以我尝试让it等待侧边栏以几种方式完成加载,包括:
var getAllLinks = function() {
casper.then(function () {
$('#search-left-inner').scrollTop(100000);
});
casper.then(function () {
var linksOnThisPage = []
//etc. etc.
}
但由于某些原因,现在它只找到一个链接,而不是20个。我想如果滚动,它不会立即加载下一个项目,因为加载需要时间。您需要在滚动后稍等片刻,然后才能再次尝试刮除所有元素
casper.start(url)
.thenEvaluate(scroll)
.wait(5000, function(){
var links = this.evaluate(getAllLinks);
this.echo(links.length);
})
.run();
如果这会产生更多链接,那么您可以尝试下一步,即无限滚动,直到没有加载新元素为止。这可以通过CasperJS中的异步递归实现:
var linkCount = -1;
function getAllLinks() {
var linksOnThisPage = []
$("a[href^='/ResumeB']").each(function(index, linkDiv) {
$linkDiv = $(linkDiv)
linksOnThisPage.push('http://www.super-resume.com' + $linkDiv.attr('href'))
});
return linksOnThisPage
}
function scroll() {
$('#search-left-inner').scrollTop(10000);
}
/**
* Returns true if more elements were loaded that were before
*/
function checkMore(){
var newLinks = this.evaluate(getAllLinks);
var newCount = newLinks.length;
if (linkCount === -1) {
linkCount = newCount;
}
return linkCount < newCount
}
/**
* Executes the a single iteration step and waits for a change in numbers.
* Terminates if there are no changes in 6 seconds.
*/
function step(){
this.thenEvaluate(scroll)
.waitFor(check, step, function _onTimeout(){
var links = this.evaluate(getAllLinks);
this.echo("finished with " + links.length + " links\n\n"+links.join("\n"));
}, 6000);
}
casper.start(url, step).run();
var linkCount=-1;
函数getAllLinks(){
var linksOnThisPage=[]
$(“a[href^='/ResumeB'])。每个(函数(索引,linkDiv){
$linkDiv=$(linkDiv)
链接到此页面。推送('http://www.super-resume.com'+$linkDiv.attr('href'))
});
返回此页上的链接
}
函数滚动(){
$(“#搜索左内”).scrollTop(10000);
}
/**
*如果加载了以前加载的更多元素,则返回true
*/
函数checkMore(){
var newLinks=this.evaluate(getAllLinks);
var newCount=newLinks.length;
如果(链接计数==-1){
linkCount=newCount;
}
返回linkCount
请记住,只有在的内部DOM上下文(页面上下文)中使用jQuery才有意义。我建议您也阅读该函数的说明
var linkCount = -1;
function getAllLinks() {
var linksOnThisPage = []
$("a[href^='/ResumeB']").each(function(index, linkDiv) {
$linkDiv = $(linkDiv)
linksOnThisPage.push('http://www.super-resume.com' + $linkDiv.attr('href'))
});
return linksOnThisPage
}
function scroll() {
$('#search-left-inner').scrollTop(10000);
}
/**
* Returns true if more elements were loaded that were before
*/
function checkMore(){
var newLinks = this.evaluate(getAllLinks);
var newCount = newLinks.length;
if (linkCount === -1) {
linkCount = newCount;
}
return linkCount < newCount
}
/**
* Executes the a single iteration step and waits for a change in numbers.
* Terminates if there are no changes in 6 seconds.
*/
function step(){
this.thenEvaluate(scroll)
.waitFor(check, step, function _onTimeout(){
var links = this.evaluate(getAllLinks);
this.echo("finished with " + links.length + " links\n\n"+links.join("\n"));
}, 6000);
}
casper.start(url, step).run();