PhantomJs:如何停止重定向到另一个URL并加载原始URL
PhantomJs: How to stop redirecting to another URL and load original URL
我试图使用PhantomJs
从forbes.com加载此URL
'http://www.forbes.com/sites/prossermarc/2016/10/14/softbanks-new-100b-tech-fund-shows-exactly-how-its-expanding-beyond-telco/#6e5027484acb'
但这是重定向到http://www.forbes.com/forbes/welcome/
,所以我不能抓住我想要的页面的标题。
我尝试中止请求,如果重定向URL是http://www.forbes.com/forbes/welcome/
,但完全停止页面加载和phantomjs
失败。
page.onResourceRequested = function(requestData, networkRequest) {
if (requestData.url.split('?')[0] === 'http://www.forbes.com/forbes/welcome/') {
networkRequest.abort();
}
};
我是否可以停止此重定向并加载原始URL?
我是否可以停止此重定向并加载原始URL?
是,尝试设置一个不同的用户代理:
page.settings.userAgent = "myAppBot"; // or simply 'bot'.
福布斯没有重定向机器人,我猜它只向普通的浏览器用户代理显示广告。
我是否可以停止此重定向并加载原始URL?
不,因为它是在服务器级完成的(头重定向)。
但是你可以点击"继续阅读文章"按钮,或者等待forbes.com在5秒左右自动重定向到文章。
我将展示如何等待:
var page = require('webpage').create();
page.viewportSize = { width: 1440, height: 900 };
page.settings.userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";
// This callback is run every time a page is done loading.
// The first time it is the welcome page, the second time it's the target page.
page.onLoadFinished = function(){
// Let's get the URL of current page
var url = page.evaluate(function(){
return document.location.href;
});
console.log(url);
// Is it the welcome page? No
if(url.indexOf("forbes/welcome") == -1)
{
setTimeout(function(){
page.render("forbes-article.png");
var title = page.evaluate(function(){
return document.querySelector("h1.article-headline").innerText;
});
console.log(title);
phantom.exit();
}, 1000);
}
// Yes it is the welcome page, let's just wait
else
{
console.log("redirected to welcome screen, waiting");
page.render("forbes-welcome.png");
}
};
page.open("http://www.forbes.com/sites/prossermarc/2016/10/14/softbanks-new-100b-tech-fund-shows-exactly-how-its-expanding-beyond-telco/#6e5027484acb");
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- ajax外部url加载完成后调用jquery函数
- 如何使用JavaScript通过URL加载页面内容
- PhantomJS未从同一URL加载.js
- 正在从oAuth后面的URL加载图像
- Fabric js从url加载svg缺少属性
- Javascript;html:有没有一种方法可以找到一个顶级的命名窗口(或选项卡)并将其带到前台,而不是将url加载到
- 将URL加载到CSS动画滑动iframe中
- 借助JS/Jquery函数,通过其URL加载youtube视频
- 我的Javascript没有'当我把一个脚本元素放在头上从URL加载它时,它不起作用
- 将Parse.File url加载到EJS模板中
- 打开一个模态(在Codeigniter中),该模态从依赖于phpvar的URL加载(在iframe中)内容
- Javascript函数根据URL加载不同的config.json文件
- 仅当从另一个 URL 加载数据时,才会触发另一个插件
- 将URL加载到iframe中并查找刚刚加载的HTML的值 - Jquery
- 以编程方式将 URL 加载到 ng 视图中
- 通过 GET URL 加载引导导航导航选项卡
- JavaScript 中的函数在通过单击 href url 加载新页面时不会调用
- 从新的 JSON URL 加载更多数据
- Javascript:从拆分字符串 url 加载多个图像