【问题标题】:PhantomJS- open page with LocalStorage by defaultPhantomJS- 默认使用 LocalStorage 打开页面
【发布时间】:2016-05-17 23:18:02
【问题描述】:

在 JavaScript DOM 操作发生后,我使用PhantomJS 获取网页的生成源。这个网页只有一个<body>,没有别的。

重要提示:此网页使用浏览器的localStorage 生成页面。

我想在打开页面之前更改 PhantomJS 中的 LocalStorage。

App.js

var page = require('webpage').create();

page.open("https://sample.com")
setTimeout(function(){
    // Where you want to save it    
    page.render("screenshoot.png")  
    // You can access its content using jQuery
    var fbcomments = page.evaluate(function(){
        return $("body").contents().find(".content") 
    }) 
    phantom.exit();
}, 1000)

【问题讨论】:

  • 您可以在page.evaluate() 中设置一些localStorage 属性。你试过了吗?有什么问题?
  • @ArtjomB。感谢您的编辑和评论。我的问题是在 page.open(url) 之前为浏览器设置 localstorage
  • 如何为尚未访问的页面设置 localStorage?
  • @ArtjomB。感谢编辑
  • @epascarello 我正在尝试在打开之前为页面设置 localStorage。

标签: javascript phantomjs local-storage


【解决方案1】:

特定域的localStorage 仅在您打开该域上的页面时可用。你可以

  1. 在您感兴趣的域上打开一些 URL,
  2. 根据您的需要更改localStorage
  3. 在同一个域中打开您的目标 URL。

这可能看起来像这样:

page.open("https://sample.com/asdfasdf", function(){
    page.evaluate(function(){
        localStorage.setItem("something", "whatever");
    });

    page.open("https://sample.com", function(){
        setTimeout(function(){
            // Where you want to save it    
            page.render("screenshoot.png")  
            // You can access its content using jQuery
            var fbcomments = page.evaluate(function(){
                return $("body").contents().find(".content") 
            }) 
            phantom.exit();
        },1000)
    });    
});

也可以在步骤 1 中不打开整个页面。您也可以使用带有一些 URL 的虚拟页面。

page.setContent("", "https://sample.com"); // doesn't actually open any page

page.evaluate(function(){
    localStorage.setItem("something", "whatever");
});

page.open("https://sample.com", function(){
    setTimeout(function(){
        // Where you want to save it    
        page.render("screenshoot.png")  
        // You can access its content using jQuery
        var fbcomments = page.evaluate(function(){
            return $("body").contents().find(".content") 
        }) 
        phantom.exit();
    }, 1000)
});    

【讨论】:

    【解决方案2】:

    我通过朋友的帮助解决了我的问题。我的解决方案是:

    注意:我为完整加载页面设置了超时 10000(10 秒)。

    var page = require('webpage').create();
    
    page.open("https://sample.com", function(){
        page.evaluate(function(){
            var i = 0,
            oJson = jsonData,
            sKey;
            localStorage.clear();
    
            for (; sKey = Object.keys(oJson)[i]; i++) {
                localStorage.setItem(sKey,oJson[sKey])
            }
        });
    
        page.open("https://sample.com", function(){
            setTimeout(function(){
             page.render("screenshoot.png") 
                // Where you want to save it    
               console.log(page.content); //page source
                // You can access its content using jQuery
                var fbcomments = page.evaluate(function(){
                    return $("body").contents().find(".content") 
                }) 
                phantom.exit();
            },10000)
        });    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 2016-08-01
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多