【问题标题】:how to access to many webpages in casperjs如何在 casperjs 中访问多个网页
【发布时间】:2017-11-15 13:07:54
【问题描述】:

我是 casperjs 的初学者,我正在尝试自动访问一个网站并抓取一些信息,我启动 url,然后将一些链接存储在一个名为“links”的表中,然后我尝试点击该表中的第一个元素(links[0]),最后测试具有 id("ctl0_CONTENU_PAGE_resultSearch_PagerTop_ctl2") 的链接是否存在。

问题是,我无法得到任何输出,我想这是因为程序首先无法访问链接(链接 [0]),这就是它找不到 id i 的原因米找。 这是我写的代码。

 var url=''; //first link
 var casper = require('casper').create();
 var links;
 var lien;// second link
 function getLinks() {
 var links = document.querySelectorAll('td a');
 return Array.prototype.map.call(links, function (e) {
  return e.getAttribute('href')
 });
 }
 casper.start(url);
 casper.then(function () {
 links = this.evaluate(getLinks);
 });

 casper.then(function () {
 lien = links[0];
 });
 casper.thenOpen(lien , function(){
 if (this.exists('a[id="ctl0_CONTENU_PAGE_resultSearch_PagerTop_ctl2"]')) {
 this.echo('the heading exists');
 }
 else {console.log('does not exist');
 }
    });
 casper.run(function () {
 this.exit();
 });`

this is the link I'm trying the find

请注意,我使用的 casperjs 版本是 1.1.4

this is what shows the console

【问题讨论】:

  • 请在您的问题中提供您正在使用的 casperjs 和 phanomjs 版本,最好提供您在控制台中的全部输出。
  • 我已将它们添加到问题中。
  • 这意味着代码在打开页面之前就中断了,在这一行casper.start(url);我会尝试在几个小时内运行它并回复你
  • 非常感谢您,我将等待您的答复
  • 但是我需要知道您使用的是哪种浏览器 phantomjs 或 slimerjs?

标签: javascript web-scraping casperjs


【解决方案1】:

为了使其工作,您需要将其包装在测试套件中。至少我是这样做的。我不是最新的 casper,我不知道过去一年是否有任何变化,但以前的问题是您无法在 v1 中没有测试套件的情况下启动 casper。

请在终端中使用以下命令开始以下代码

casperjs test filename.js

在你的 filename.js 中加入以下代码

var url = 'https://www.marchespublics.gov.ma/pmmp/';
var url2 = 'https://www.marchespublics.gov.ma/index.php5?page=entreprise.EntrepriseAdvancedSearch&AllCons&EnCours&domaineActivite=1.15';

casper.test.begin('Scraping start', 3, function(test) {
    casper.start(url, function() {
        this.test.pass('Opened 1st page');
    })
    .thenOpen(url2, function(){
        this.test.pass('Opened 2nd page')
    })
    .then(function() {
        if (this.exists('a[id="ctl0_CONTENU_PAGE_resultSearch_PagerTop_ctl2"]')) {
            this.test.pass('the heading exists');
        } else {
            this.test.fail('Does not exist');
        }
    })
    .run(function() {
        test.done();
    });
});

有关 casperjs 在执行脚本时的更多详细信息,请尝试使用类似这样的附加参数运行它

casperjs test filename.js --verbose --log-level=debug

希望有帮助

编辑 1:

无需测试套件也可以使用,只需调用此代码即可

casperjs filename.js

var casper = require('casper').create();
var url = 'https://www.marchespublics.gov.ma/index.php5?page=entreprise.EntrepriseAdvancedSearch&AllCons&EnCours&domaineActivite=1.15';

casper
    .start(url, function() {
        this.echo('Opened page')
    })
    .then(function() {
        if (this.exists('a[id="ctl0_CONTENU_PAGE_resultSearch_PagerTop_ctl2"]')) {
            this.echo('the heading exists');
        } else {
            this.echo('Does not exist');
        }
    })
    .run();

【讨论】:

  • 我更新了我的答案,因为现在有更多时间尝试,所以请随意尝试新的 sn-p
猜你喜欢
  • 1970-01-01
  • 2014-06-16
  • 1970-01-01
  • 1970-01-01
  • 2010-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多