{"id":3046,"date":"2013-08-18T09:50:29","date_gmt":"2013-08-18T01:50:29","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=3046"},"modified":"2013-09-26T09:11:40","modified_gmt":"2013-09-26T01:11:40","slug":"%e5%88%a9%e7%94%a8phantomjs%e8%bf%9b%e8%a1%8c%e7%bd%91%e9%a1%b5%e6%88%aa%e5%9b%be","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/3046.html","title":{"rendered":"\u5229\u7528phantomjs\u8fdb\u884c\u7f51\u9875\u622a\u56fe"},"content":{"rendered":"<p><span style=\"font-size: small;\">\u5728\u5f00\u53d1\u4e2d\uff0c\u6709\u8fd9\u6837\u4e00\u4e2a\u9700\u6c42\uff0c\u5c31\u662f\u5206\u4eab\u67d0\u4e2a\u7f51\u9875\u5185\u5bb9\u5230\u5fae\u535a\u4e0a\uff0c\u9700\u8981\u5c06\u7f51\u9875\u622a\u56fe\u4e0b\u6765\uff0c\u5f53\u5fae\u535a\u914d\u56fe\u53d1\u4e0a\u53bb\u3002phantomjs\u521a\u597d\u53ef\u4ee5\u6ee1\u8db3\u8fd9\u4e2a\u9700\u6c42\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">phantomjs\u5730\u5740\uff1a https:\/\/github.com\/ariya\/phantomjs\/<\/span><\/p>\n<p><span style=\"font-size: small;\">ubuntu\u4e2d\u5b89\u88c5phantomjs\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nsudo apt-get install phantomjs\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u65b0\u5efa\u4e00\u4e2ajs\u6587\u4ef6 github.js\uff1a<\/span><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar page = require('webpage').create();\r\npage.open('http:\/\/github.com\/', function () {\r\n    page.render('github.png');\r\n    phantom.exit();\r\n});\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7136\u540e\u8fd0\u884c\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nphantomjs github.js\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4f20\u9012\u53c2\u6570\u7ed9js\u6587\u4ef6\uff1a<\/span><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/\u7528phantom\u5e93\uff0c\u5c06html\u9875\u9762\u4fdd\u5b58\u4e3a\u56fe\u7247\r\n\/\/ \u53c2\u65701 \u4e3a\u7f51\u9875\u5b8c\u6574\u7684url\u5730\u5740  \u53c2\u65702 \u4e3a\u56fe\u7247\u4fdd\u5b58\u7684\u5b8c\u6574\u8def\u5f84\r\nvar page = require('webpage').create();\r\n\r\nvar args = require('system').args;\r\nargs.forEach(function(arg, i) {\r\n    if (i==1){\r\n        address = arg;\r\n    }\r\n    else if(i==2){\r\n        dest_filename = arg;\r\n    }\r\n});\r\n\r\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\/10A5355d Safari\/8536.25';\r\n\/\/page.customHeaders = {'Referer': 'localhost'};\r\npage.open(address, function () {\r\n    page.render(dest_filename);\r\n    phantom.exit();\r\n});\r\nconsole.log(dest_filename);\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u53ef\u4ee5\u901a\u8fc7python\u6765\u8c03\u7528phantomjs\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef timeout_command(command, timeout):\r\n\r\n    &quot;&quot;&quot;call shell-command and either return its output or kill it\r\n    if it doesn't normally exit within timeout seconds and return None&quot;&quot;&quot;\r\n\r\n    cmd = command\r\n    start = datetime.datetime.now()\r\n    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\r\n    while process.poll() is None:\r\n        time.sleep(0.2)\r\n        now = datetime.datetime.now()\r\n        if (now - start).seconds&gt; timeout:\r\n            try:\r\n                os.kill(process.pid, signal.SIGKILL)\r\n                os.waitpid(-1, os.WNOHANG)\r\n            except:\r\n                pass\r\n            return ''\r\n    return process.stdout.readlines()\r\n\r\n''' \u5c06\u9875\u9762\u4fdd\u5b58\u4e3a\u56fe\u7247 '''\r\ndef html2jpg(url):\r\n    filename = '%s.jpg' % time.time()\r\n    imgdir = '\/tmp\/screenshots\/'\r\n    if not os.path.isdir(imgdir):\r\n        os.makedirs(imgdir)\r\n    filepath = os.path.join(imgdir, filename)\r\n    jspath = os.path.join(os.path.dirname(__file__), 'html2jpg.js')\r\n    cmd = 'phantomjs %s &quot;%s&quot; %s' % (jspath, url, filepath)\r\n    ret = timeout_command(cmd, timeout=5)\r\n    if not ret:\r\n        return None\r\n    ret = ret&#x5B;0].strip()\r\n    if not str(ret) == str(filepath):\r\n        return None\r\n    img_url = '\/tmp\/screenshots\/%s' % filename\r\n    return img_url\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5982\u679c\u8fd0\u884c phantomjs \u547d\u4ee4\u51fa\u73b0\u7c7b\u4f3c X Server\u6253\u5f00\u5931\u8d25\u7684\u9519\u8bef\uff0c\u5c06phantomjs\u5347\u7ea7\u5230\u6700\u65b0\u7248\u672c\u5c31\u597d\u4e86\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u89e3\u51b3\u4e2d\u6587\u5b57\u4f53\u95ee\u9898\uff1aapt-get install xfonts-wqy\u00a0<\/span><\/p>\n<p><span style=\"font-size: small;\">\u77e5\u4e4e\u7528\u7684\u53e6\u4e00\u4e2a\u5e93 wkhtmltojpg<\/span><\/p>\n<p><span style=\"font-size: small;\">http:\/\/www.zhihu.com\/question\/21455769<\/span><\/p>\n<p><span style=\"font-size: small;\">python\u6709\u4e2a\u7c7b\u4f3c\u7684\u5e93\uff1a<\/span><\/p>\n<p><span style=\"font-size: small;\">https:\/\/github.com\/jeanphix\/Ghost.py<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e0d\u8fc7\u6211\u8bd5\u4e86\u4e00\u4e0b\uff0c\u597d\u50cf\u6709\u4e9b\u95ee\u9898\uff0c\u8fd8\u4e0d\u592a\u7a33\u5b9a\u548c\u6210\u719f\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5f00\u53d1\u4e2d\uff0c\u6709\u8fd9\u6837\u4e00\u4e2a\u9700\u6c42\uff0c\u5c31\u662f\u5206\u4eab\u67d0\u4e2a\u7f51\u9875\u5185\u5bb9\u5230\u5fae\u535a\u4e0a\uff0c\u9700\u8981\u5c06\u7f51\u9875\u622a\u56fe\u4e0b\u6765\uff0c\u5f53\u5fae\u535a\u914d\u56fe\u53d1\u4e0a\u53bb\u3002phantomj [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3046","post","type-post","status-publish","format-standard","hentry","category-diary"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/3046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=3046"}],"version-history":[{"count":4,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/3046\/revisions"}],"predecessor-version":[{"id":3109,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/3046\/revisions\/3109"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=3046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=3046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=3046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}