繁体   English   中英

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

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

直接来自page.evaluate()的文档

在网页的上下文中评估给定的函数。 执行是沙箱,网页无法访问幻像对象,无法探测自己的设置。

无需进一步说明。

扩展Tomalak的答案:

您的evaluate() ed函数不是在Phantoms脚本的上下文中运行,而是在页面中运行,因此它无法看到fs

在这种情况下,您将希望您的函数以其他方式读取脚本的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM