我正在尝试从以下链接下载html:
当我在chrome中打开时,它会将所有想要下载的数据加载到html中。我想用幻影打开这些页面,但它们加载的不一样吗?我正在使用下面的代码来拍摄幻影加载的截图。它只是主要的匹配历史页面:http://matchhistory.na.leagueoflegends.com/en
var page = require('webpage').create();
var url="http://matchhistory.na.leagueoflegends.com/en/#match-details/TRLH3/1000430019?gameHash=a5e39c76a8e91ba9&tab=stats";
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent';
page.open(url, function(status) {
if (status !== 'success') {
console.log('Unable to access network');
}
setTimeout(function (){page.render('mh.png');},1000);
setTimeout(function (){phantom.exit();},1200);
});
我不知道他们为什么提出两种不同的东西。我怎样才能让pahntomjs渲染相同的东西呢?
提前感谢
发布于 2018-08-03 19:56:06
正如@andrew所指出的,之所以会发生这种情况,是因为PhantomJS在处理重定向时删除了片段。存在一个问题(https://github.com/ariya/phantomjs/issues/12192)和为修复(https://github.com/ariya/phantomjs/pull/14941)创建的拉请求,但由于PhantomJS暂停了开发(https://github.com/ariya/phantomjs/issues/15344),这些请求尚未发布。
另一种方法是使用木偶词典(https://github.com/GoogleChrome/puppeteer),它有一个关于如何捕捉截图的用法示例。
在您的例子中,这可以像安装Puppeteer一样简单:
npm install puppeteer
然后将代码更新为:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://matchhistory.na.leagueoflegends.com/en/#match-details/TRLH3/1000430019?gameHash=a5e39c76a8e91ba9&tab=stats');
await page.screenshot({path: 'mh.png'});
await browser.close();
})();
并通过node
而不是phantomjs
运行代码。
node <filename>.js
网站有更多关于可以配置的信息(视图、端口等)。
发布于 2018-08-03 17:25:46
您的http
链接可能被重定向到https
。我的猜测是,phantom.js没有保留片段标识符( #match-details
)或重定向之后的任何内容,这就是为什么要获取主页http://matchhistory.na.leagueoflegends.com/en
的原因。
要解决您的问题,请使用https
链接,因为您不会被重定向,这将有效。
var url="https://matchhistory.na.leagueoflegends.com/en/#match-details/TRLH3/1000430019?gameHash=a5e39c76a8e91ba9&tab=stats";
https://stackoverflow.com/questions/51680681
复制相似问题