javascript - NodeJS x-ray web-scraper : how to follow links and get content from sub page

标签 javascript node.js web-scraping web-crawler x-ray

所以我正在尝试使用 node.js X 射线抓取框架来抓取一些内容。虽然我可以从单个页面获取内容,但我不知道如何跟踪链接并一次性从子页面获取内容。

X 射线 github 配置文件上有一个示例,但如果我将代码更改到其他站点,它会返回空数据。

我已经简化了我的代码,并让它爬取了这个示例的 SO 问题。

以下工作正常:

var Xray = require('x-ray');
var x = Xray();

x('http://stackoverflow.com/questions/9202531/minimizing-nexpectation-for-a-custom-distribution-in-mathematica', '#content', [{

  title: '#question-header h1',
  question: '.question .post-text'

}])
(function(err, obj) {

  console.log(err);
  console.log(obj);

})

这也有效:

var Xray = require('x-ray');
var x = Xray();

x('http://stackoverflow.com/questions', '#questions .question-summary .summary', [{

  title: 'h3',
  question: x('h3 a@href', '#content .question .post-text'),

}])
(function(err, obj) {

  console.log(err);
  console.log(obj);

})

但这给了我空的详细信息结果,我不知道出了什么问题:

var Xray = require('x-ray');
var x = Xray();

x('http://stackoverflow.com/questions', '#questions .question-summary .summary', [{

  title: 'h3',
  link: 'h3 a@href',
  details: x('h3 a@href', '#content', [{
    title: 'h1',
    question: '.question .post-text',
  }])

}])
(function(err, obj) {

  console.log(err);
  console.log(obj);

})

我希望我的蜘蛛抓取包含列出的问题的页面,然后点击每个问题的链接并检索其他信息。

最佳答案

所以在一些帮助下,我找出了问题所在。我发布这个答案以防其他人可能有同样的问题。

工作示例:

var Xray = require('x-ray');
var x = Xray();

x('http://stackoverflow.com/questions', '#questions .question-summary .summary', [{

  title: 'h3',
  link: 'h3 a@href',
  details: x('h3 a@href', {
    title: 'h1',
    question: '.question .post-text',
  })

}])
(function(err, obj) {

  console.log(err);
  console.log(obj);

})

关于javascript - NodeJS x-ray web-scraper : how to follow links and get content from sub page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31802903/

相关文章:

mysql - 在 sequelize.js 中,是否可以取消嵌套包含嵌套的查询结果?

node.js - 无法使用 coinbase pro api 检索某些信息(订单历史记录、交易账户)

python - Scrapy 不使用我当前的语法返回网页的文本正文

html - VBA Web Scraping 使用 getElementsByClassName 获取名称和地址

javascript - 将 "value"从表单中的选择传递到具有 javascript 问题的 php 变量

node.js - npm 安装时出现 MODULE_NOT_FOUND 错误

javascript - 页面重新加载未触发文档就绪

python - 如何抓取具有特定选项值的选项

javascript - 如何读取xml中另一个标签下标签的属性

javascript - Function 和 new Function 的区别