【问题标题】:Exclude children of the first span tag inside div排除 div 内第一个 span 标签的子项
【发布时间】:2016-05-15 19:30:36
【问题描述】:

我正在构建一个简单的网络爬虫。这是我正在抓取的网站 - http://www.home.com/pro/c/oho,-NI。我的爬虫点击每个类名为pro-title 的链接,并从它进入的网页中提取数据(例如: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

【问题讨论】:

  • 请在问题中包含您正在谈论的标记。页面可能会发生变化,您的问题和答案将对未来的读者失去所有价值。

标签: javascript css-selectors phantomjs casperjs


【解决方案1】:

还可以包括 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']
});

【讨论】:

  • 在浏览器的开发工具中测试 jquery 选择器,您会发现它们可以正常工作。有没有下载jquery放到正确的目录下?
  • 是的,他们工作,但我不认为他们在 CasperJS 中工作。我将 jquery 下载到正确的目录中。
  • 它被命名为 jquery.js 吗?测试 $ 是否在评估函数中定义。
  • 请查看我的编辑...我在原始帖子中遗漏了一个括号,该括号需要包含 jquery。
  • 我第一次运行您的代码时包含了括号。它没有用。
猜你喜欢
  • 2015-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-10
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多