gpt4 book ai didi

javascript - 在 PhantomJS 中截取手机网站

转载 作者:行者123 更新时间:2023-11-29 21:09:17 24 4
gpt4 key购买 nike

我正在使用此脚本截取我希望成为 iPhone 尺寸的移动网站的屏幕截图:

var system = require('system');
var args = system.args;

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

page.open(args[1], function () {

page.viewportSize = { width: 414, height: 736 };

page.clipRect = { top: 0, left: 0, width: 414, height: 736 };
page.render(args[2]);
console.log(args[2]);
phantom.exit();
});

这在某些网站上似乎可行,但在其他网站上却错误地显示了实际情况。

例如,这是它为 Google 生成的内容:

enter image description here

Google 显然有一个移动网站,那么我哪里出错了?

最佳答案

与UserAgent有关

这里有关于如何设置它的信息:http://phantomjs.org/api/webpage/property/settings.html

这段代码对我有用:

var system = require('system');
var args = system.args;

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

page.settings.userAgent = 'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30';

page.open("http://www.google.com", function () {

page.viewportSize = { width: 414, height: 736 };

page.clipRect = { top: 0, left: 0, width: 414, height: 736 };
page.render("./test.jpg");

phantom.exit();
});

关于javascript - 在 PhantomJS 中截取手机网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42478979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com