在javascript

时间:2019-07-11 03:08:06

标签: php phantomjs

我想编写一些能够获取javascript值的php代码(包括已安装的phantomjs代码和phantomjs),如果我想将javascript的值获取到php怎么办?

javascript

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

page.open('https://www.lazada.com.my/products/xiaomi-original-mi-backpack-simple-urban-life-style-bag-dark-grey-i116600345-s121584557.html?spm=a2o4k.searchlist.list.4.41755f1dNNJa0E&search=1',function(){ 
    var name = page.evaluate(function(){
        return document.getElementsByClassName("pdp-mod-product-badge-title") 
        [0].value;
    });
    document.write(name);
});
phantom.exit();

1 个答案:

答案 0 :(得分:0)

在网络浏览器中运行此代码段无效,我们需要使用PhantomJS加载此脚本。因此,我们将此代码段保存在文件test.js中,可以是您要命名的任何代码。然后从命令行运行。

phantomjs test.js

希望您已经安装了phantomjs。 https://scotch.io/tutorials/what-is-phantomjs-and-how-is-it-used

尝试一下:

    page.open('https://www.lazada.com.my/products/xiaomi-original-mi- 
        backpack-simple-urban-life-style-bag-dark-grey-i116600345- 
        s121584557.html?spm=a2o4k.searchlist.list.4.41755f1dNNJa0E&search=1', 
        function(){ 
        var name = page.evaluate(function(){
            return document.getElementsByClassName("pdp-mod-product-badge-title") 
            [0].value;
        });

        return name;
        phantom.exit();
    });