抓取没有id、类、属性等的动态元素中的文本

Scrape text inside dynamic element with no id, class, attributes, etc

本文关键字:动态 元素 文本 属性 id 抓取      更新时间:2023-09-26

我唯一得到的是前面的<td>将始终具有相同的(并且对文档来说是唯一的)内容:

<td>  
    <label>unique text<label>  
</td>  
<td>dynamic text</td>

我可以很容易地在浏览器控制台中使用jQuery抓取它(页面已经加载了jQuery):

$("label:contains('unique text')").parent().next().text();

我已经做了一段时间了,我已经尝试了我能想到的所有方法。

我最近的尝试是使用casperjs的评估和:

casper.thenEvaluate(function addID() {  
    $("label:contains('unique text')").parent().next().attr('id', 'uniqueID');  
});  
casper.then(function getText() {  
    var target = this.getHTML('td#uniqueID');  
    this.echo(target);  
});

这给了我:

CasperError: No element matching selector found: td#uniqueID

为什么我的casper.thenEvaluate函数没有创建我正在寻找的td#uniqueID ?

如果我像这个帖子的答案那样做:

casper.then(function getText() {  
    this.evaluate(function addID() {  
        $("label:contains('unique text')").parent().next().attr('id', 'uniqueID');  
    });
    var target = this.thenEvaluate(function returnText() {  
        return $('#uniqueID').text();
    });
    this.echo(target);
});

我得到了一个[Object Casper],这似乎正是它听起来的样子。它被waitForContent, scrollTo等填充…

注:上面的代码块是不正确的(正如Artjom b在这个答案中指出的那样),并更改为:

casper.then(function getText() {  
    this.evaluate(function addID() {  
        $("label:contains('unique text')").parent().next().attr('id', 'uniqueID');  
    });
    var target = this.fetchText('#uniqueID');
    this.echo(target);
});  

问题仍然存在。

如果你已经尝试过了,为什么不直接复制呢?您的错误是在then块内使用thenEvaluate。CasperJS分步骤工作,您安排了一个不必要的步骤。这将创建另一个稍后执行的步骤。

thenEvaluate更改为evaluate,应该可以正常工作。这时,您可以将两者结合起来:

casper.then(function getText() {  
    var target = this.evaluate(function addID() {  
        $("label:contains('unique text')").parent().next().attr('id', 'uniqueID');  
        return $('#uniqueID').text();
    });
    this.echo(target);
});

或者

casper.then(function getText() {  
    this.evaluate(function addID() {  
        $("label:contains('unique text')").parent().next().attr('id', 'uniqueID');  
    });
    var target = this.fetchText(#uniqueID);
    this.echo(target);
});

我终于解决了。当我在浏览器中访问页面时,我单击链接,javascript通过ajax POST调用我正在抓取的信息的模态。我假设在casperjs中,当我使用:

时,它将以相同的方式工作。
casper.thenOpen('http://www.website.net/details.php', {
    method: 'post',
    data:   {
        'id': 'foo',
        'key':  'bar',
    }
});

看起来casper的渲染效果与我预期的略有不同。尽管主页加载了jQuery,但模态没有。所以我需要:

clientScripts: ["jquery-2.1.1.min.js"]

在我:

var casper = require('casper').create({
});

(casperjs docs on jQuery)

这里是完整的脚本供参考:

var url = "http://www.website.net/search.php?key=foobarfoobarfoobarfoobar";
var casper = require('casper').create({
    clientScripts: ["jquery-2.1.1.min.js"]
});
casper.start(url, function() {
    //some other unrelated stuff is going to go here
});
casper.thenOpen('http://www.website.net/details.php', {
    method: 'post',
    data:   {
        'id': 'foo',
        'key':  'bar',
    }
});
casper.then(function getText() {  
    this.evaluate(function addID() {  
        $("label:contains('unique text')").parent().next().attr('id', 'uniqueID');  
    });
    var target = this.fetchText('#uniqueID');
    this.echo(target);
});  
casper.run();