简体   繁体   English

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

[英]PhantomJS Issue writing to file fs. Can't find variable: fs

I am trying out phantomJS for the first time and i have successfully extracted som data from a site, but when i try to write some content to a file i get the error: ReferenceError: Can't find variable: fs 我是第一次尝试phantomJS并且我已成功从站点中提取som数据,但是当我尝试将某些内容写入文件时,我得到错误:ReferenceError:找不到变量:fs

Here is my script 这是我的剧本

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

What am i missing out here? 我在这里错过了什么?

Edit: Of the response on this question i learned why i couldnt reach the data inside the evalute, and how i could access it. 编辑:在这个问题的回答中,我了解了为什么我无法访问评估中的数据,以及我如何访问它。

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

You can't have phantomjs objects in page.evaluate because that is a webpage. 你不能在page.evaluate拥有phantomjs对象,因为那是一个网页。 I'll give you a simple example how can you achieve what you are doing. 我将举一个简单的例子,说明你如何实现自己的目标。

If you want to write some content of webpage in a file you have to return those contens from page.evaluate . 如果要在文件中编写某些webpage内容,则必须从page.evaluate返回这些page.evaluate and you will get those values in page.open . 你将在page.open获得这些值。 Here you have access to fs , so you can write those contents. 在这里您可以访问fs ,因此您可以编写这些内容。

I'm showing with a simple example how you can you write some webpage title to a file. 我用简单的例子展示了如何将一些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();
            });
        }
    });

Straight from the docs for page.evaluate() : 直接来自page.evaluate()的文档

Evaluates the given function in the context of the web page. 在网页的上下文中评估给定的函数。 The execution is sandboxed, the web page has no access to the phantom object and it can't probe its own setting. 执行是沙箱,网页无法访问幻像对象,无法探测自己的设置。

No further explanation needed. 无需进一步说明。

Expanding on Tomalak's answer: 扩展Tomalak的答案:

Your evaluate() ed function isn't run in the context of your Phantoms script, but in the page, so it can't see fs . 您的evaluate() ed函数不是在Phantoms脚本的上下文中运行,而是在页面中运行,因此它无法看到fs

In this, case, you'll want your function to read the results of your script in some other way. 在这种情况下,您将希望您的函数以其他方式读取脚本的结果。

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

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