Using PhantomJS for webpage screenshots perfectly solves the issue of intercepting height

Source: Internet
Author: User

Using PhantomJS for webpage screenshots perfectly solves the issue of intercepting height
PhantomJS is a WebKit-based server-side JavaScript API. It fully supports the web without the support of the browser. It is fast and native supports various Web standards: DOM processing, CSS selector, JSON, Canvas, and SVG. PhantomJS can be used for page automation, network monitoring, webpage screenshots, and non-interface tests. We can also use it for crawling. As you know, some data on the Web page is rendered by executing js, so it will be very troublesome for crawlers to capture data, phantomJS comes with the WebKit kernel. We can use PhantomJS to solve the problem that crawlers cannot execute js. This time I want to talk about his function. below is the official website's rasterize. js example: var page = require ('webpage '). create (), system = require ('system'), address, output, size; if (system. args. length <3 | system. args. length> 5) {console. log ('usage: rasterize. js URL filename [paperwidth * paperheight | paperformat] [zoom] '); console. log ('paper (pdf output) examples: "5in * 7.5in", "10 cm * 20 cm", "A4", "Letter" '); console. log ('image (png/jpg output) ex Amples: "1920px" entire page, window width 1920px '); console. log ('"800px * 600px" window, clipped to 800x600'); phantom. exit (1);} else {address = system. args [1]; output = system. args [2]; page. viewportSize = {width: 600, height: 600}; if (system. args. length> 3 & system. args [2]. substr (-4) = ". pdf ") {size = system. args [3]. split ('*'); page. paperSize = size. length = 2? {Width: size [0], height: size [1], margin: '0px '}:{ format: system. args [3], orientation: 'portrait', margin: '000000'};} else if (system. args. length> 3 & system. args [3]. substr (-2) = "px") {size = system. args [3]. split ('*'); if (size. length = 2) {pageWidth = parseInt (size [0], 10); pageHeight = parseInt (size [1], 10); page. viewportSize = {width: pageWidth, height: pageHeight}; // use clipRec T specifies the rendering area: page. clipRect = {top: 0, left: 0, width: pageWidth, height: pageHeight};} else {console. log ("size:", system. args [3]); pageWidth = parseInt (system. args [3], 10); pageHeight = parseInt (pageWidth * 3/4, 10); // it's as good an assumption as any console. log ("pageHeight:", pageHeight); page. viewportSize = {width: pageWidth, height: pageHeight };}} if (system. args. length> 4) {page. zo OmFactor = system. args [4];} page. open (address, function (status) {if (status! = 'Success') {console. log ('unable to load the address! '); Phantom. exit (1);} else {window. setTimeout (function () {page. render (output); phantom. exit () ;}, 200) ;}}) ;}the above code can be performed, but the problem is that we need to manually specify the page height, which is inconvenient. A buddy found in the garden to solve this problem by manually setting the height. The idea is that PhantomJS itself can also execute js. After loading the page, we can get the actual height of the page and reset the area to be intercepted. The following code is available: // execute a script on the page to obtain the page rendering height (var bb = page. evaluate (function () {return document. getElementsByTagName ('html ') [0]. getBoundingClientRect () ;}); // you can specify the page width and height based on the actual page height. clipRect = {top: bb. top, left: bb. left, width: bb. width, height: bb. height}; // reserve a certain rendering time window. setTimeout (function () {page. render (file); page. close (); console. log ('Render OK ');}, 1000); the transformed code is as follows: var page = require ('webpage '). create (), system = require ('system'), address, output, size; if (system. args. length <3 | system. args. length> 5) {console. log ('usage: rasterize. js URL filename '); phantom. exit (1);} else {address = system. args [1]; output = system. args [2]; page. viewportSize = {width: 1024, height: 600}; page. open (address, function (status) {// obtain the rendering height var bb = page by running a script on the page. evaluate (function () {return document. getElementsByTagName ('html ') [0]. getBoundingClientRect () ;}); // you can specify the page width and height based on the actual page height. clipRect = {top: bb. top, left: bb. left, width: bb. width, height: bb. height}; // reserve a certain rendering time window. setTimeout (function () {page. render (output); page. close (); console. log ('Render OK ') ;}, 1000 );});}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.