2

我一直在尝试为我们正在构建的网站抓取 10 个网站,其中包含指向原始网站的链接,在 node.js 上使用cheerio,我们得到的问题是一些网站已经改变,现在使用 ajax 调用来带来他们的数据,我的问题是我们如何获取这些信息,例如先触发按钮单击然后获取 DOM。

其次:相同的 dom 结构并没有获取我所有的数据,它正在检索一个页面的信息,但没有获取具有相同 DOM 结构的另一页面上的元素。任何帮助,将不胜感激。

谢谢并恭祝安康。 编辑1:相关代码

$('#ProductContent').filter(function(){
                            var price = undefined;
                            var ukulele = false;
                            var model = $(this).find('.ProductSubtitle').text().replace(/\n\s*/g,"");
                            if(model.indexOf(/m/i) != 0){
                                var description = $(this).find('.RomanceCopy').text().replace(/\n\s*|\r/g,"");
                                .
                                .code removed for brevity and the variables present here are populated
                                .
                                //this children is populated only for one page.
                                children =  $(this).find('.SpecsColumn .SpecsTable table tbody').children('tr');
                                console.log('children: '+children.length)
                                console.log(guitar_url);
                                children.each(function(){
                                    var key = $(this).children('td').first().text();
                                    var value = $(this).children('td').last().text();
                                    specs[key] = value;
                                    console.log(specs); 
                                });

编辑 2: Cherios 初始化

request(guitar_url,function(error,response,html){
                    if(!error){
                        var $ = cheerio.load(html);
                        $("#content #right-content").filter(function(){..children and other variables are populated inside here....})
                    }
 })
4

2 回答 2

5

总结您收到的所有评论:

Cheerio 是受 jQuery 启发的简约 DOM 阅读器。它的设计专注于读取数据,而不是浏览器模拟器,您可以在其中单击按钮。

替代方法是使用PhantomJSCasperJS等无头浏览器。

这两个超出了 Node.js 的范围,您可能很难将数据从 Node.js 来回传输到无头浏览器。

如果保留在 Node.js 环境中对您很重要,那么您可以使用JSDOM

它们都比 Cheerio 使用起来更复杂,但是如果你想操作 DOM,在 DOM 上执行 JavaScript 等等……那么这是你最好的选择。

于 2014-07-10T09:05:57.813 回答
0

删除“tbody”标签解决了这个问题,一旦它们被删除,它就开始正常获取所有三个站点的数据。

于 2014-07-14T06:46:41.477 回答