javascript – PhantomJS写入文件fs的问题.找不到变量: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: [],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() {

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


kindeditor4.x代码高亮功能默认使用的是prettify插件,prettify是Google提供的一款源代码语法高亮着色器,它提供一种简单的形式来着色HTML页面上的程序代码,实现方式如下: 首先在编辑器里面插入javascript代码: 确定后会在编辑器插入这样的代码: <pre
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代码高亮,因为SyntaxHighlighter的应用非常广泛,所以将kindeditor默认的prettify替换为SyntaxHighlighter代码高亮插件 上一篇“让kindeditor显示高亮代码”中已经
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小
JS怎么获取当前时间戳
JS如何判断对象是否为数组
JS怎么获取图片当前宽高
JS对象如何转为json格式字符串
JS怎么获取图片原始宽高
怎么在click事件中调用多个js函数
js如何往数组中添加新元素
js如何拆分字符串
JS怎么对数组内元素进行求和
JS如何判断屏幕大小
js怎么解析json数据
js如何实时获取浏览器窗口大小
原生JS实现别踩白块小游戏(五)
原生JS实现别踩白块小游戏(一)