下面是代碼,我以前用來解析爲E-店的物品,並把HTML代碼對這些項目
我希望它會幫助你的每一頁!對於你的問題------ CODE OF
var RenderUrlsToFile, system, url_string_for_array;
var arrayOfUrls = new Array();
system = require("system");
RenderUrlsToFile = function(urls, callbackPerUrl, callbackFinal) {
var getFilename, next, page, retrieve, urlIndex, webpage, link_name, sex;
var fs = {};
fs = require('fs');
urlIndex = 0;
webpage = require("webpage");
page = null;
// getFilename = function() {
// return "parsed/" + urlIndex + ".png";
// };
next = function(status, url, file) {
page.close();
callbackPerUrl(status, url, file);
return retrieve();
};
retrieve = function() {
var url;
if (urls.length > 0) {
url = urls.shift();
urlIndex++;
page = webpage.create();
page.viewportSize = {
width: 800,
height: 600
};
page.settings.userAgent = "Phantom.js bot";
return page.open("http://" + url, function(status) {
var file;
// file = getFilename();
if (status === "success") {
return window.setTimeout((function() {
// page.render(file);
var js = page.evaluate(function() {
return document;
});
fs.write('your_file_path'.html', js.all[0].outerHTML, 'w');
return next(status, url, file);
}), 100);
} else {
return next(status, url, file);
}
});
} else {
return callbackFinal();
}
};
return retrieve();
};
if (system.args.length > 1) {
arrayOfUrls = Array.prototype.slice.call(system.args, 1);
} else {
------------主要部分
例如:我需要解析的電子商店的物品,所以我拿的第一頁,然後我設置「的」爲頁面完全相同numbe
url_string_for_array = "www.lamoda.ru/c/559/accs-muzhskieaksessuary/?genders=men&page=1";
for(var k=2; k<20; k++)
{
url_string_for_array += ",www.lamoda.ru/c/559/accs-muzhskieaksessuary/?genders=men&page="+k;
}
arrayOfUrls = url_string_for_array.split(',');
}
RenderUrlsToFile(arrayOfUrls, (function(status, url, file) {
if (status !== "success") {
return console.log("Unable to render '" + url + "'");
} else {
return console.log("Rendered '" + url + "'");
}
}), function() {
return phantom.exit();
});
您可以創建更多的'page'實例?也許是一個數組? – Unsigned
即時通訊不知道... beacuse頁面必須打開很長一段時間...也是一個開始在JavaScript ..可以解釋我嗎? – MOB
@未簽名可以告訴我一些例子嗎? – MOB