{"id":3491,"date":"2017-02-07T16:15:51","date_gmt":"2017-02-07T08:15:51","guid":{"rendered":"http:\/\/blog.hoyo.idv.tw\/?p=3491"},"modified":"2017-02-07T16:15:51","modified_gmt":"2017-02-07T08:15:51","slug":"phantomjs-%e4%b8%80%e5%80%8b%e5%8f%af%e4%bb%a5%e4%bd%bf%e7%94%a8%e6%8c%87%e4%bb%a4%e6%93%b7%e5%8f%96%e7%b6%b2%e9%a0%81%e5%8f%8a%e5%b0%87-ajax-%e7%b6%b2%e9%a0%81%e8%bd%89%e7%82%ba%e5%8f%af-seo","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=3491","title":{"rendered":"PhantomJS - \u4e00\u500b\u53ef\u4ee5\u4f7f\u7528\u6307\u4ee4\u64f7\u53d6\u7db2\u9801\u53ca\u5c07 ajax \u7db2\u9801\u8f49\u70ba\u53ef SEO \u7684\u7a0b\u5f0f"},"content":{"rendered":"<p>Full web stack\u00a0No browser required<\/p>\n<ul>\n<li><a href=\"http:\/\/phantomjs.org\/\" target=\"_blank\">PhantomJS\u00a0<\/a><\/li>\n<\/ul>\n<p>--<\/p>\n<p><strong>\u5b89\u88dd<\/strong><\/p>\n<p>\u53c3\u8003\u81f3<\/p>\n<ul>\n<li><a href=\"https:\/\/gist.github.com\/ryanoboril\/278197b04cb8c838d72b\" target=\"_blank\">Setup PhantomJS on CentOS 6.5<\/a><\/li>\n<\/ul>\n<ol>\n<li>http:\/\/phantomjs.org\/download.html \u9078\u64c7 64 \u4f4d\u5143<\/li>\n<li>sudo yum install freetype fontconfig<\/li>\n<li>wget\u00a0https:\/\/bitbucket.org\/ariya\/phantomjs\/downloads\/phantomjs-2.1.1-linux-x86_64.tar.bz2<\/li>\n<li>bunzip2 phantomjs*.tar.bz2<\/li>\n<li>tar xvf phantomjs*.tar<\/li>\n<li>sudo cp phantomjs*\/bin\/phantomjs \/usr\/bin\/phantomjs<\/li>\n<li>phantomjs -v<\/li>\n<\/ol>\n<p>--<\/p>\n<p><strong>\u53d6\u5f97 ajax \u5f8c\u7684\u5b8c\u6574\u539f\u59cb\u78bc<\/strong><\/p>\n<p>\u53c3\u8003\u81f3<\/p>\n<ul>\n<li><a href=\"https:\/\/www.mxgw.info\/t\/phantomjs-prerender-for-seo.html\" target=\"_blank\">\u7528PhantomJS\u6765\u7ed9AJAX\u7ad9\u70b9\u505aSEO\u4f18\u5316<\/a><\/li>\n<\/ul>\n<pre class=\"lang:js decode:true \">\"use strict\";\r\n\r\n\/\/ \u5355\u4e2a\u8d44\u6e90\u7b49\u5f85\u65f6\u95f4\uff0c\u907f\u514d\u8d44\u6e90\u52a0\u8f7d\u540e\u8fd8\u9700\u8981\u52a0\u8f7d\u5176\u4ed6\u8d44\u6e90\r\nvar resourceWait = 500;\r\nvar resourceWaitTimer;\r\n\r\n\/\/ \u6700\u5927\u7b49\u5f85\u65f6\u95f4\r\nvar maxWait = 5000;\r\nvar maxWaitTimer;\r\n\r\n\/\/ \u8d44\u6e90\u8ba1\u6570\r\nvar resourceCount = 0;\r\n\r\n\/\/ PhantomJS WebPage\u6a21\u5757\r\nvar page = require('webpage').create();\r\n\r\n\/\/ NodeJS \u7cfb\u7edf\u6a21\u5757\r\nvar system = require('system');\r\n\r\n\/\/ \u4eceCLI\u4e2d\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u76ee\u6807URL\r\nvar url = system.args[1];\r\n\r\n\/\/ \u8bbe\u7f6ePhantomJS\u89c6\u7a97\u5927\u5c0f\r\npage.viewportSize = {\r\n    width: 1280,\r\n    height: 1014\r\n};\r\n\r\n\/\/ \u83b7\u53d6\u955c\u50cf\r\nvar capture = function(errCode){\r\n\r\n    \/\/ \u5916\u90e8\u901a\u8fc7stdout\u83b7\u53d6\u9875\u9762\u5185\u5bb9\r\n    console.log(page.content);\r\n\r\n    \/\/ \u6e05\u9664\u8ba1\u65f6\u5668\r\n    clearTimeout(maxWaitTimer);\r\n\r\n    \/\/ \u4efb\u52a1\u5b8c\u6210\uff0c\u6b63\u5e38\u9000\u51fa\r\n    phantom.exit(errCode);\r\n\r\n};\r\n\r\n\/\/ \u8d44\u6e90\u8bf7\u6c42\u5e76\u8ba1\u6570\r\npage.onResourceRequested = function(req){\r\n    resourceCount++;\r\n    clearTimeout(resourceWaitTimer);\r\n};\r\n\r\n\/\/ \u8d44\u6e90\u52a0\u8f7d\u5b8c\u6bd5\r\npage.onResourceReceived = function (res) {\r\n\r\n    \/\/ chunk\u6a21\u5f0f\u7684HTTP\u56de\u5305\uff0c\u4f1a\u591a\u6b21\u89e6\u53d1resourceReceived\u4e8b\u4ef6\uff0c\u9700\u8981\u5224\u65ad\u8d44\u6e90\u662f\u5426\u5df2\u7ecfend\r\n    if (res.stage !== 'end'){\r\n        return;\r\n    }\r\n\r\n    resourceCount--;\r\n\r\n    if (resourceCount === 0){\r\n\r\n        \/\/ \u5f53\u9875\u9762\u4e2d\u5168\u90e8\u8d44\u6e90\u90fd\u52a0\u8f7d\u5b8c\u6bd5\u540e\uff0c\u622a\u53d6\u5f53\u524d\u6e32\u67d3\u51fa\u6765\u7684html\r\n        \/\/ \u7531\u4e8eonResourceReceived\u5728\u8d44\u6e90\u52a0\u8f7d\u5b8c\u6bd5\u5c31\u7acb\u5373\u88ab\u8c03\u7528\u4e86\uff0c\u6211\u4eec\u9700\u8981\u7ed9\u4e00\u4e9b\u65f6\u95f4\u8ba9JS\u8dd1\u89e3\u6790\u4efb\u52a1\r\n        \/\/ \u8fd9\u91cc\u9ed8\u8ba4\u9884\u7559500\u6beb\u79d2\r\n        resourceWaitTimer = setTimeout(capture, resourceWait);\r\n\r\n    }\r\n};\r\n\r\n\/\/ \u8d44\u6e90\u52a0\u8f7d\u8d85\u65f6\r\npage.onResourceTimeout = function(req){\r\n    resouceCount--;\r\n};\r\n\r\n\/\/ \u8d44\u6e90\u52a0\u8f7d\u5931\u8d25\r\npage.onResourceError = function(err){\r\n    resourceCount--;\r\n};\r\n\r\n\/\/ \u6253\u5f00\u9875\u9762\r\npage.open(url, function (status) {\r\n\r\n    if (status !== 'success') {\r\n\r\n        phantom.exit(1);\r\n\r\n    } else {\r\n\r\n        \/\/ \u5f53\u6539\u9875\u9762\u7684\u521d\u59cbhtml\u8fd4\u56de\u6210\u529f\u540e\uff0c\u5f00\u542f\u5b9a\u65f6\u5668\r\n        \/\/ \u5f53\u5230\u8fbe\u6700\u5927\u65f6\u95f4\uff08\u9ed8\u8ba45\u79d2\uff09\u7684\u65f6\u5019\uff0c\u622a\u53d6\u90a3\u4e00\u65f6\u523b\u6e32\u67d3\u51fa\u6765\u7684html\r\n        maxWaitTimer = setTimeout(function(){\r\n\r\n            capture(2);\r\n\r\n        }, maxWait);\r\n\r\n    }\r\n\r\n});\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u5be6\u969b\u4f7f\u7528<\/strong><\/p>\n<pre class=\"lang:default decode:true\">phantomjs spider.js http:\/\/www.bocelife.tw<\/pre>\n<p>&nbsp;<\/p>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"3491\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,441&nbsp;total views, &nbsp;2&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Full web stack\u00a0...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"3491\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,441&nbsp;total views, &nbsp;2&nbsp;views today<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/3491"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3491"}],"version-history":[{"count":1,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/3491\/revisions"}],"predecessor-version":[{"id":3492,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/3491\/revisions\/3492"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}