我正在尝试在 Meteor 上使用 X 射线,但到目前为止还没有运气。
这是我正在测试的示例(它在基本节点应用程序上运行良好)
import Xray from 'x-ray';
var xray = new Xray();
xray('http://reddit.com/r/meteor/', '.title',
[{
title: '',
href: '@href'
}]
)
.write('./result.json');
我正在尝试在 Meteor 上使用 X 射线,但到目前为止还没有运气。
这是我正在测试的示例(它在基本节点应用程序上运行良好)
import Xray from 'x-ray';
var xray = new Xray();
xray('http://reddit.com/r/meteor/', '.title',
[{
title: '',
href: '@href'
}]
)
.write('./result.json');
我希望您在 5 个月前就已经弄清楚了,我对这个问题有所了解,并以这种方式弄清楚了。
不要使用大气包,因为它不再维护。
$meteor npm install --save x-ray
(https://github.com/lapwinglabs/x-ray)
然后只需在服务器端创建一个 Meteor.method 并在客户端调用它。
(https://docs.meteor.com/api/methods.html)
// Server Side
import Xray from 'x-ray'
Meteor.methods({
scrap:function(){
var x = Xray();
console.log('Is scrapping');
x('http://google.com', 'title')(function(err, title) {
console.log(title) // Google
})
}
});
然后
// Client Side
Meteor.apply('scrap', function(error, result) {
console.log(error, result);
console.log('Scrap is bussy');
})
干杯
The code from the previous post indeed invokes the x-ray function on the server side but doesn't return the result to the client.
Using async/wait and promises (ES7) you can return the result from the server to the client:
method.js (server):
import { Meteor } from 'meteor/meteor';
import Xray from 'x-ray';
Meteor.methods({
async 'scrape.test'() {
let x = Xray(),
scraper;
function scrap() {
return new Promise((r, e) => {
x('http://google.com', 'title')(function(err, title) {
if (err) e(err);
if (title) r(title);
});
});
}
try {
return await scrap();
} catch (error) {
throw new Meteor.Error('500', error);
}
}
});
client.js:
Meteor.call('scrape.test', (error, result) => {
if (error) console.log('error', error);
console.log('result', result);
});
Cheers