我們正在製作一個簡單的腳本來瀏覽網站的站點地圖,並獲取所有鏈接和href值,然後將其保存到.json列表中,這是另一個模塊可以用來截取這些訪問過的頁面的屏幕截圖。使用節點騎馬人將節點鏈接保存爲.json
到目前爲止,我們可以獲得抓取列表的功能。當它在控制檯中運行時,我們想要放入數組的數據顯示出來。
在終端中運行時,沒有發現任何內容,並且該數組未填充。在測試運行時
var fs = require('fs');
var Horseman = require('node-horseman');
var horseman = new Horseman();
function findAllUrls(selector) {
var urls = [];
// get all the anchors
$(selector).each(function() {
// loop through each anchor and get the href value
var url = {
title: $(this).text(),
url: $(this).attr("href")
};
// put the href value in a new array
urls.push(url);
});
// finally return the array of all the href value
console.log("Log all urls from findAllUrls", urls);
return urls;
};
horseman
.open(URL goes here)
.evaluate(findAllUrls, '.sitemap-links a')
.then(function(urls) {
console.log(urls);
// Save the urls to a json file (lookup node 'fs' module)
fs.writeFile('urls.json', urls, function (err) {
if (err) throw err;
console.log('saved to urls.json');
});
})
.close();
東西是越來越跳過。我有一種感覺,就是用PhantomJS模擬瀏覽器,而不是讓數組繼續通過。