【问题标题】:CasperJS' getElementsByXpath returns null with valid XPathCasperJS 的 getElementsByXpath 返回具有有效 XPath 的 null
【发布时间】:2016-01-03 15:47:45
【问题描述】:

我正在从网站上抓取足球比分数据。所有分数都在一个表格中,每个<tr> 都有“阻止主场比赛 17”和一些独特的东西。

我在 Chrome 开发工具中测试了我的 xpath,它只识别我需要的表格行。

var utils = require('utils');
var casper = require('casper').create();
var xpath = require('casper').selectXPath;
var result = [];

function getScores(){
    console.log("getting scores");
    result = __utils__.getElementsByXPath("//tr[contains(@id,'block_home_matches_17')");
}

casper.start('http://int.soccerway.com/', function() {
    console.log("casper start....");       
    var l = getScores();
    utils.dump(l);
});

casper.run();

代码返回 [] 作为 utils.dump!为什么?我的 xpath 有效!

【问题讨论】:

标签: javascript xpath web-scraping phantomjs casperjs


【解决方案1】:

你有三个问题:

您可以通过 CasperJS 函数检索目标 DOM 节点的表示

casper.start('http://int.soccerway.com/', function() {
    utils.dump(this.getElementsInfo(xpath("//tr[contains(@id,'block_home_matches_17')")));
});

或直接处理页面上下文中的元素:

casper.start('http://int.soccerway.com/', function() {
    utils.dump(this.evaluate(function(){
        return __utils__.getElementsByXPath("//tr[contains(@id,'block_home_matches_17')").map(function(el){
            return {} // TODO: produce your own representation
        });
    }));
});

【讨论】:

  • 此外,您不会从getScores() 返回任何内容,因此l 将始终为空。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-11
  • 2016-06-24
  • 1970-01-01
  • 1970-01-01
  • 2017-02-05
  • 2018-05-29
  • 1970-01-01
相关资源
最近更新 更多