排除 div 内第一个 span 标签的子标签

Exclude children of the first span tag inside div

我正在构建一个简单的网络抓取工具。这是我正在抓取的网站 - http://www.home.com/pro/c/oho,-NI. My scraper clicks on every link with a class name of pro-title and extracts data from the web page it enters (eg: http://www.me.com/pro/home/marcelle-services)

var casper = require('casper').create({
    logLevel:"verbose",
    debug:true
});

var jsonObj = {};
var links;
var name;
var paragraph;
var contact;
var description;
var location;
var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
var regex = new RegExp(expression);

casper.start('http://www.home.com/ro/c/oho,-TN');

casper.then(function getLinks(){
     links = this.evaluate(function(){
        var links = document.getElementsByClassName('pro-title');
        links = Array.prototype.map.call(links,function(link){
            return link.getAttribute('href');
        });
        return links;
    });
});
casper.then(function(){
    this.each(links,function(self,link){
      if (link.match(regex)) {
        self.thenOpen(link,function(a){
          var location = this.fetchText('div.info-list-text');
          //var location = document.querySelectorAll("div.info-list-text")[1];
          var contact = this.fetchText('span.pro-contact-text');
          var description = this.fetchText('div.profile-about div');
          this.echo(location);
          //this.echo(contact);
          //this.echo(description);
        });
      }
    });
});
casper.run(function(){
    this.exit();
});

上面的代码产生了这个输出,

                                       Professionals

                                Interior Decorators

                Contact: GuilbeauLocation: 5007 Wyoming Ave.Nowoah, MI 45786

我想省略 div.info-list-text a span:first 选择,这样单词 Professionals 就不会被记录。

还可以包含 jQuery,让您在选择元素时更轻松。一种解决方案:

var casper = require('casper').create({
   logLevel:"verbose",
   debug:true,
    clientScripts:  ['jquery.js']
});

var jsonObj = {};
var links;
var name;
var paragraph;
var contact;
var description;
var location;
var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
var regex = new RegExp(expression);

casper.start('http://www.houzz.com/professionals/c/Nashville,-TN');

casper.then(function getLinks(){
 links = this.evaluate(function(){
    var links = document.getElementsByClassName('pro-title');
    links = Array.prototype.map.call(links,function(link){
        return link.getAttribute('href');
    });
    return links;
});
});

casper.then(function(){
 this.each(links,function(self,link){
  if (link.match(regex)) {
    self.thenOpen(link,function(a){

       // I just manually extracted the stuff you wanted with jquery selectors
       var txtYouWant = casper.evaluate(function() {
          var desiredText = $($("div.info-list-text").first().find("span a span")[1]).text();
          desiredText += $($("div.info-list-text")[1]).text();
          desiredText += $($("div.info-list-text")[2]).text();
          return desiredTxt;
       });
    });
  }
});
});

编辑:

确保修复此部分:

var casper = require('casper').create({
   logLevel:"verbose",
   debug:true,
   clientScripts:  ['jquery.js']
});