尝试使用 PhantomJS 处理网页时出现问题

aLf*_*Lfa 1 javascript phantomjs

我正在尝试为 SEO 目的制作一个爬虫,但我似乎无法让 PhantomJS 至少下载这个特定页面: https: //tablet.euroslots.com/home/

\n\n

如果我使用 cURL 它工作正常(但显然不处理 javascript):

\n\n
\xe2\x9c\x93 1344:0 /cherrytech/js-crawler root\xe2\x80\xba curl https://tablet.euroslots.com/home/\n<!doctype html><!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7"> ...\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的 PhantomJS 脚本:

\n\n
var page = require(\'webpage\').create();\n\npage.onResourceRequested = function (request) {\n  console.log(\'Request \' + JSON.stringify(request, undefined, 4));\n};\n\npage.onResourceReceived = function(response) {\n  console.log(\'Response (#\' + response.id + \', stage "\' + response.stage + \'"): \' + JSON.stringify(response));\n};\n\npage.onResourceError = function(resourceError) {\n  console.log(\'Unable to load resource (#\' + resourceError.id + \'URL:\' + resourceError.url + \')\');\n  console.log(\'Error code: \' + resourceError.errorCode + \'. Description: \' + resourceError.errorString);\n};\n\npage.settings.userAgent = \'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A406 Safari/8536.25\';\npage.open(\'https://tablet.euroslots.com/home/\', function() {\n  console.log(page.content);\n  phantom.exit();\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是运行它的结果:

\n\n
\xe2\x9c\x93 1347:0 /cherrytech/js-crawler root\xe2\x80\xba phantomjs crawler.js\nRequest {\n    "headers": [\n        {\n            "name": "User-Agent",\n            "value": "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A406 Safari/8536.25"\n        },\n        {\n            "name": "Accept",\n            "value": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"\n        }\n    ],\n    "id": 1,\n    "method": "GET",\n    "time": "2014-09-16T16:02:24.426Z",\n    "url": "https://tablet.euroslots.com/home/"\n}\nUnable to load resource (#1URL:https://tablet.euroslots.com/home/)\nError code: 2. Description: Connection closed\nResponse (#1, stage "end"): {"contentType":null,"headers":[],"id":1,"redirectURL":null,"stage":"end","status":null,"statusText":null,"time":"2014-09-16T16:02:24.763Z","url":"https://tablet.euroslots.com/home/"}\n<html><head></head><body></body></html>\n
Run Code Online (Sandbox Code Playgroud)\n

小智 5

尝试使用 --ssl-protocol=any 调用 phantomjs

\n\n

我也遇到了同样的问题,一个外部网站一周前还可以工作。

\n\n

所以我搜索了一下,发现了Qt QNetworkReply 连接关闭中描述的相关问题。它帮助我研究了 phantomjs 的嵌入式 Qt:它默认强制在 SSLv3 中建立新连接,这对于旧站点来说太新,或者对于新站点来说太旧(但在 Qt 4.8.4 时是相当合理的默认值)被释放)。

\n\n

使用“any”,您告诉 phantomjs 尝试所有协议,这应该可以帮助您通过测试。它将尝试比 SSLv3 更安全的协议,但也会尝试比 SSLv3 安全性更低的协议(SSLv3 处于中等范围)。因此,如果“any”有效,那么您应该尝试强制使用比 SSLv3 更安全的值,而不是让“any”。就我而言,指定 --ssl-protocol=tlsv1 有效。

\n\n

猜测最近的 SSL 问题(goto 失败、heartbleed、poodle 等)使得很多网站升级了服务器,现在拒绝 SSLv3 连接。\n但如果您的服务器使用旧于 SSLv3 的协议,请保留“任何”(以及所有相关的安全风险\xe2\x80\xa6)。

\n