【发布时间】:2014-08-31 11:57:28
【问题描述】:
我一直在尝试为我们正在构建的网站抓取 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....}) } })
【问题讨论】:
-
展示你如何检索 DOM 的代码,以及为什么你很难触发按钮点击?
-
我不知道如何使用cheerios从我的服务器触发按钮点击,我将通过编辑我的问题发布代码
-
为避免被标记,我建议从问题中删除您要报废的网站。
-
谢谢,我想您需要知道我遇到的问题是哪个网站,才能更好地理解代码。
-
Cheerio 不会在页面中加载 javascript,您也无法与之交互(点击等)。页面上使用 javascript 的任何内容都无法访问,ajax 就是其中之一。
标签: node.js screen-scraping cheerio