我是JavaScript的新手,所以要有耐心。我一直在尝试抓取一个站点,并获取一个列表中的所有产品URL,稍后我将在其他函数中使用这些URL:
url='https://www.fromuthtennis.com/frm/c-10-mens-tops.aspx'
var http = require('http-get');
var request = require("request");
var cheerio = require("cheerio");
function getURLS(url) {
request(url, function(err, resp, body){
var linklist = [];
$ = cheerio.load(body);
var links = $('#productResults a');
for(valor in links) {
if(links[valor].attribs && links[valor].attribs.href && linklist.indexOf(links[valor].attribs.href) == -1){
linklist.push(links[valor].attribs.href);
}
}
var extended_links = [];
linklist.forEach(function(link){
extended_link = 'https://www.fromuthtennis.com/frm/' + link;
extended_links.push(extended_link);
})
console.log(extended_links);
})
};这是有效的,除非你转到第二页这样的项目:
url='https://www.fromuthtennis.com/frm/c-10-mens-tops.aspx#Filter=[pagenum=2*ava=1]'
var http = require('http-get');
var request = require("request");
var cheerio = require("cheerio"); //etc...据我所知,这是因为页面上的内容是动态加载的。为了获得页面的内容,我认为我需要使用PhantomJS,因为它允许我在页面完全加载后获得html代码,所以我安装了phantomjs-node模块。我想使用NodeJS来获取URL列表,因为我其余的代码都是在上面写的。
我已经读了很多关于PhantomJS的文章,但是使用phantomjs-node很棘手,我仍然不明白如何使用它获得网址列表,因为我对JavaScript或一般的编码都是非常陌生的。
如果有人能给我一点指导,我将不胜感激。
发布于 2016-10-28 12:10:04
可以,停那儿吧。该页面看起来像是实现了Google's Ajax Crawling URL。
基本上,它允许网站为Google生成爬虫友好的内容。每当您看到这样的URL时:
https://www.fromuthtennis.com/frm/c-10-mens-tops.aspx#Filter=[pagenum=2*ava=1]您需要将其转换为:
https://www.fromuthtennis.com/frm/c-10-mens-tops.aspx?_escaped_fragment_=Filter%3D%5Bpagenum%3D2*ava%3D1%5D转换只需采用基本路径:https://www.fromuthtennis.com/frm/c-10-mens-tops.aspx,添加一个查询参数_escaped_fragment_,该参数的值是使用标准URI编码的Filter=[pagenum=2*ava=1]编码成Filter%3D%5Bpagenum%3D2*ava%3D1%5D的URL片段。
您可以在此处阅读完整的规范:https://developers.google.com/webmasters/ajax-crawling/docs/specification
注意:这并不适用于所有网站,只适用于实现Google的Ajax爬行URL的网站。但在这种情况下你很幸运,
发布于 2016-10-28 11:39:52
你可以在不使用动态内容的情况下使用这个url来查看任何你想要的产品:
https://www.fromuthtennis.com/frm/showproduct.aspx?ProductID={product_id}
例如,要查看产品37023:
https://www.fromuthtennis.com/frm/showproduct.aspx?ProductID=37023
你所要做的就是for(var productid=0;prodcutid<40000;productid++) {request...}。
另一种方法是使用幻影模块。(https://www.npmjs.com/package/phantom)。它将允许您直接从NodeJS应用程序运行幻影命令
https://stackoverflow.com/questions/40297582
复制相似问题