无法在CasperJS中打开Facebook

Cannot open Facebook in CasperJS

本文关键字:Facebook CasperJS      更新时间:2023-09-26
var picCounter = 1
    , url = 'https://www.facebook.com/video.php?v=10152880761616654'

var casper = require('casper').create({
    verbose: false
, logLevel: 'debug'
, stepTimeout: 15000
})
casper.start()
casper.userAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')
casper.thenOpen(url, function() {
    console.log('in casper.start')
})
casper.wait(3000, function() { 
    console.log('capturing after start')
    console.log(this.getCurrentUrl())
})
casper.run()

这是日志

[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url: https://www.facebook.com/video.php?v=101528807616
16654, HTTP GET
[debug] [phantom] Navigation requested: url=https://www.facebook.com/video.php?v
=10152880761616654, type=Other, lock=true, isMainFrame=true
[warning] [phantom] Loading resource failed with status=fail: https://www.facebo
ok.com/video.php?v=10152880761616654
[debug] [phantom] Successfully injected Casper client-side utilities
in casper.start
[info] [phantom] Step 2/3: done in 151ms.
[info] [phantom] Step 3/3: done in 160ms.
[info] [phantom] wait() finished waiting for 3000ms.
capturing after start
about:blank
[info] [phantom] Done 3 steps in 3166ms

getCurrentUrl()不断返回about:blank.

有什么我可以尝试让它工作吗?我的目标是转到状态/照片/视频等的一些页面,以从该页面获取评论。

我正在使用CasperJS 1.0.4和PhantomJS 1.8.2

从 1.8.2 版本开始修复了许多错误。我建议你使用PhantomJS 1.9.8。此外,CasperJS的1.1.0-beta3版本在1.0.4上有很多改进,并且基本稳定。

这可能不是必需的,但是在PhantomJS中使用Internet Explorer用户代理字符串是一个坏主意,因为该页面可能会提供针对IE优化的JavaScript/CSS,而这些JavaScript/CSS在PhantomJS中不起作用。一个例子是谷歌。始终使用适当的用户代理,例如 PhantomJS 1.x 的代理:

Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1