[英]PhantomJS Issue writing to file fs. Can't find variable: fs
我是第一次嘗試phantomJS並且我已成功從站點中提取som數據,但是當我嘗試將某些內容寫入文件時,我得到錯誤:ReferenceError:找不到變量:fs
這是我的劇本
var page = require('webpage').create();
var fs = require('fs');
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.open("http://www.pinterest.com/search/pins/?q=motorbike", function(status) {
if (status === "success") {
page.includeJs("http://code.jquery.com/jquery-latest.js", function() {
page.evaluate(function() {
var imgs = {
title: [],
href: [],
ext: [],
src: [],
alt: []
};
$('a.pinImageWrapper').each(function() {
imgs.title.push($(this).attr('title'));
imgs.href.push($(this).attr('href'));
var ext = $(this).children('.pinDomain').html();
imgs.ext.push(ext);
var img = $(this).children('.fadeContainer').children('img.pinImg');
imgs.src.push(img.attr('src'));
imgs.alt.push(img.attr('alt'));
});
if (imgs.title.length >= 1) {
for (var i = 0; i < imgs.title.length; i++) {
console.log(imgs.title[i]);
console.log(imgs.href[i]);
console.log(imgs.ext[i]);
console.log(imgs.src[i]);
console.log(imgs.alt[i]);
}
} else {
console.log('No pins found');
}
fs.write('foo.txt', 'bar');
});
phantom.exit();
});
}
});
我在這里錯過了什么?
編輯:在這個問題的回答中,我了解了為什么我無法訪問評估中的數據,以及我如何訪問它。
var page = require('webpage').create();
var fs = require('fs');
page.onConsoleMessage = function(msg) {
console.log(msg);
};
openPinPage('motorbike');
function openPinPage(keyword) {
page.open("http://www.pinterest.com/search/pins/?q=" + keyword, function(status) {
if (status === "success") {
page.includeJs("http://code.jquery.com/jquery-latest.js", function() {
getImgsData();
});
}
});
}
function getImgsData() {
var data = page.evaluate(function() {
var imgs = {
title: [],
href: [],
ext: [],
src: [],
alt: []
};
$('a.pinImageWrapper').each(function() {
imgs.title.push($(this).attr('title'));
imgs.href.push($(this).attr('href'));
var ext = $(this).children('.pinDomain').html();
imgs.ext.push(ext);
var img = $(this).children('.fadeContainer').children('img.pinImg');
imgs.src.push(img.attr('src'));
imgs.alt.push(img.attr('alt'));
});
return imgs;
});
for (var i = 0; i < data.title.length; i++) {
console.log(data.title[i]);
};
phantom.exit();
}
你不能在page.evaluate
擁有phantomjs
對象,因為那是一個網頁。 我將舉一個簡單的例子,說明你如何實現自己的目標。
如果要在文件中編寫某些webpage
內容,則必須從page.evaluate
返回這些page.evaluate
。 你將在page.open
獲得這些值。 在這里您可以訪問fs
,因此您可以編寫這些內容。
我用簡單的例子展示了如何將一些webpage
標題寫入文件。
page.open("http://www.pinterest.com/search/pins/?q=motorbike", function(status) {
if (status === "success") {
page.includeJs("http://code.jquery.com/jquery-latest.js", function() {
var title = page.evaluate(function() {
return document.title; // here I don't have access to fs I'll return title of document from here.
});
console.log(title) //I got the title now I can write here.
fs.write('foo.txt', title);
phantom.exit();
});
}
});
擴展Tomalak的答案:
您的evaluate()
ed函數不是在Phantoms腳本的上下文中運行,而是在頁面中運行,因此它無法看到fs
。
在這種情況下,您將希望您的函數以其他方式讀取腳本的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.