所以我正在尝试使用 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/