【问题标题】:PhantomJS: specify User Agent when making a callPhantomJS:拨打电话时指定用户代理
【发布时间】:2013-10-03 12:27:26
【问题描述】:

我正在使用 PhantomJS 调用网页,如下所示:

page.open('http://example.com', function (s) {
  console.log(page.content);
  phantom.exit();
});

我在 Drupal Simpletests 的上下文中使用它,这需要我设置一个特殊的 USERAGENT 才能使用测试数据库而不是真实数据库。我想获取特定用户代理的网页。例如,在带有 Curl 的 PHP 中,我可以在调用 cUrl 之前使用 CURLOPT_USERAGENT 执行此操作。

谢谢!

阿尔伯特

【问题讨论】:

    标签: phantomjs


    【解决方案1】:

    我在phantomjs examples directoryuseragent.js文件中找到了答案:

    var page = require('webpage').create();
    console.log('The default user agent is ' + page.settings.userAgent);
    page.settings.userAgent = 'SpecialAgent';
    page.open('http://www.httpuseragent.org', function (status) {
        if (status !== 'success') {
            console.log('Unable to access network');
        } else {
            var ua = page.evaluate(function () {
                return document.getElementById('myagent').innerText;
            });
            console.log(ua);
        }
        phantom.exit();
    });
    

    【讨论】:

    • 我无法访问网络。我可以通过其他浏览器访问该站点。它是内部可访问的站点。是否有一些设置我必须设置才能从 Phantom 访问。
    猜你喜欢
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多