简体   繁体   English

如何使用casperJS简洁地检查404的网页资源?

[英]How can I concisely check a webpage's resources for 404s using casperJS?

I have just started using Phantom/Casper. 我刚刚开始使用Phantom / Casper。

So far I can list all the resources that a page has using this code: 到目前为止,我可以使用以下代码列出页面拥有的所有资源:

casper.on('resource.received', function (resource) {
        casper.echo(resource.url);
   });

So far so good. 到现在为止还挺好。

Now im trying to merge this with a chunk of code I gleaned and mashed from the documentation. 现在,我试图将其与我从文档中收集并混搭的大量代码合并。 I wanted to load each resource, and then print out the URL if it was missing: 我想加载每个资源,如果缺少则打印出URL:

casper.on('resource.received', function (resource) {
    //        casper.echo(resource.url);
    casper.Open(resource.url, function (resource) {
        this.on('http.status.404', function (resource) {
            this.echo('missing:' + resource.url);
        });
    });
});

It's messy, but it's what I've got. 杂乱无章,但这就是我所拥有的。 It fails to open the resources (ln 3), and the console shows no activity. 它无法打开资源(ln 3),并且控制台未显示任何活动。

How can I rewrite this to iterate over the resources and check them for 404s? 如何重写此代码以遍历资源并检查它们的404?

(I am aware in my example that I'm not iterating over the resources, I was tempted to use eachthen(), but It's not clear if I can use general casperJS methods inside the 'test' prototype. Sorry, I hope this wasn't too long) (在我的示例中,我知道我没有遍历资源,我很想使用eachthen(),但是尚不清楚我是否可以在“测试”原型中使用通用的casperJS方法。对不起,我希望这不是时间不长)

CasperJS' resource.received is based on PhantomJS' onResourceReceived . CasperJS的resource.received基于PhantomJS的onResourceReceived As you can see from the documentation, you can simply access resource.status . 从文档中可以看到,您只需访问resource.status There is no need to explicitly load the resource. 无需显式加载资源。

casper.on('resource.received', function (resource) {
    if (resource.stage === "end" && resource.status === 404) {
        this.echo('missing:' + resource.url);
    };
});

Btw, you probably mean casper.open and not casper.Open . 顺便说一句,您可能是说casper.open而不是casper.Open

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM