Node.js使用Jsdom进行Web抓取
我想刮的网站,以获得最后获胜的5个数字和两颗星。可以在网站的左栏看到。我一直在阅读教程,但我没有能力做到这一点 这是我到目前为止写的代码:Node.js使用Jsdom进行Web抓取,node.js,web-scraping,jsdom,Node.js,Web Scraping,Jsdom,我想刮的网站,以获得最后获胜的5个数字和两颗星。可以在网站的左栏看到。我一直在阅读教程,但我没有能力做到这一点 这是我到目前为止写的代码: app.get('/winnernumbers', function(req, res){ //Tell the request that we want to fetch youtube.com, send the results to a callback function request({uri: 'http://www.eu
app.get('/winnernumbers', function(req, res){
//Tell the request that we want to fetch youtube.com, send the results to a callback function
request({uri: 'http://www.euromillones.com.es/ '}, function(err, response, body){
var self = this;
self.items = new Array();//I feel like I want to save my results in an array
//Just a basic error check
if(err && response.statusCode !== 200){console.log('Request error.');}
//Send the body param as the HTML code we will parse in jsdom
//also tell jsdom to attach jQuery in the scripts and loaded from jQuery.com
jsdom.env({
html: body,
scripts: ['http://code.jquery.com/jquery-1.6.min.js ']
}, function(err, window){
//Use jQuery just as in a regular HTML page
var $ = window.jQuery;
res.send($('title').text());
});
});
});
我得到以下错误:
必须将“created”、“loaded”、“done”选项或回调传递给jsdom.env。在我看来,您刚刚使用了一组jsdom不知道如何处理的参数。显示此签名:
jsdom.env(string, [scripts], [config], callback);
中间的两个参数是可选的,但您会注意到这里所有可能的组合都以字符串开头,以回调结束。文档中提到了调用jsdom.env
的另一种方法,即通过传递单个config
参数。您所做的工作相当于:
jsdom.env(config, callback);
不符合任何记录的方法。我建议更改代码以传递单个配置参数。您可以将当前回调移动到该配置对象的done
字段。大概是这样的:
jsdom.env({
html: body,
scripts: ['http://code.jquery.com/jquery-1.6.min.js'],
done: function (err, window) {
//Use jQuery just as in a regular HTML page
var $ = window.jQuery;
res.send($('title').text());
}
});
我就是这样解决的:jsdom.env({url:,scripts:[',done:function(err,window){//像在常规HTML页面var$=window.jQuery;res.send($('title').text();}});2018调用并说:“jsdom.env未定义”