gpt4 book ai didi

javascript - PhantomJS 问题写入文件 fs。找不到变量 : fs

转载 作者:数据小太阳 更新时间:2023-10-29 05:02:27 28 4
gpt4 key购买 nike

我是第一次尝试 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 对象,因为那是一个网页。我会给你一个简单的例子,你如何才能实现你正在做的事情。

如果你想在文件中写入一些网页的内容,你必须从page.evaluate返回这些内容。您将在 page.open 中获得这些值。在这里您可以访问 fs,因此您可以编写这些内容。

我将通过一个简单示例展示如何将一些网页 标题写入文件。

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();
});
}
});

关于javascript - PhantomJS 问题写入文件 fs。找不到变量 : fs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24867176/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com