phontomjs webpage模块属性

2024-05-07 02:08

本文主要是介绍phontomjs webpage模块属性,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

随时随地技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

canGoBack

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.canGoBack example.

canGoForward

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.canGoForward example.

clipRect

clipRect {object}

This property defines the rectangular area of the web page to be rasterized when page.render is invoked. If no clipping rectangle is set, page.render will process the entire web page.

Examples

var webPage = require('webpage');
var page = webPage.create();page.clipRect = {top: 14,left: 3,width: 400,height: 300
};

Issues

clipRect doesn’t seem to be applied for JPEG render exports, works with PNG though.

指定截图的区域,若没有指定默认整个页面。

content

content {string}

This property stores the content of the web page (main frame), enclosed in an HTML/XML element. Setting the property will effectively reload the web page with the new content.

See also page.plainText to get the content without any HTML tags.

Examples

var webPage = require('webpage');
var page = webPage.create();page.open('http://phantomjs.org', function (status) {var content = page.content;console.log('Content: ' + content);phantom.exit();
});

cookies

Get or set Cookies visible to the current URL (though, for setting, use of page.addCookieis preferred). This array will be pre-populated by any existing Cookie data visible to this URL that is stored in the CookieJar, if any.

Cookies is an array of objects:

{ domain: 'example.com',expires: 'Sat Oct 11 2014 21:44:33 GMT+0200 (CEST)',expiry: 1476128618,httponly: false,name: 'cookieName',path: '/',secure: false,value: cookieValue
}

Examples

var webPage = require('webpage');
var page = webPage.create();page.open('http://phantomjs.org', function (status) {var cookies = page.cookies;console.log('Listing cookies:');for(var i in cookies) {console.log(cookies[i].name + '=' + cookies[i].value);}phantom.exit();
});

customHeaders

customHeaders {object}

Introduced: PhantomJS 1.5

This property specifies additional HTTP request headers that will be sent to the server for every request issued (for pages and resources). The default value is an empty object {}. Headers names and values get encoded in US-ASCII before being sent. Please note that the ‘User-Agent’ should be set using the page.settings, setting the ‘User-Agent’ property in this property will overwrite the value set via page.settings.

Examples

Send two additional headers ‘X-Test’ and ‘DNT’

var webPage = require('webpage');
var page = webPage.create();page.customHeaders = {"X-Test": "foo","DNT": "1"
};

Do you only want these customHeaders passed to the initial page.open request?

Here’s the recommended workaround:

var webPage = require('webpage');
var page = webPage.create();page.customHeaders = {"X-Test": "foo","DNT": "1"
};page.onInitialized = function() {page.customHeaders = {};
};

event

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.event example.

focusedFrameName

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.focusedFrameName example.

content {string}

This property stores the content of the web page (main frame), enclosed in an HTML/XML element. Setting the property will effectively reload the web page with the new content.

See also page.plainText to get the content without any HTML tags.

Examples

var webPage = require('webpage');
var page = webPage.create();page.open('http://phantomjs.org', function (status) {var content = page.content;console.log('Content: ' + content);phantom.exit();
});

frameContent

frameContent {string}

Introduced: PhantomJS 1.7

This property stores the content of the web page’s currently active frame (which may or may not be the main frame), enclosed in an HTML/XML element.

Setting the property will effectively reload the web page with the new content.

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.frameContent example.

frameName

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.frameName example.

framePlainText

framePlainText {string}

Introduced: PhantomJS 1.7

Read-only. This property stores the content of the web page’s currently active frame (which may or may not be the main frame) as plain text — no element tags!

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.framePlainText example.

frameTitle

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.frameTitle example.

frameUrl

frameUrl {string}

Introduced: PhantomJS 1.7

Read-only. This property gets the current URL of the web page’s currently active frame (which may or may not be the main frame).

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.frameUrl example.

framesCount

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.framesCount example.

framesName

Returns array with frames names.

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.framesName example.

libraryPath

libraryPath {string}

This property stores the path which is used by page.injectJs function to resolve the script name.

Initially it is set to the location of the script invoked by PhantomJS.

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.libraryPath example.

navigationLocked

navigationLocked {boolean}

This property defines whether navigation away from the page is permitted or not. If it is set to true, then the page is locked to the current URL. Defaults to false.

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.navigationLocked example.

设置为true后,该页面锁定为当前url。

offlineStoragePath

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.offlineStoragePath example.

ownsPages

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.ownsPages example.

pagesWindowName

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.pagesWindowName example.

pages

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.pages example.

paperSize

paperSize {object}

This property defines the size of the web page when rendered as a PDF.

Values and measurements

paperSize

If no paperSize is defined, the size is defined by the web page.

Supported dimension units are: 'mm''cm''in''px'. No unit means 'px'.

Margin

(Previously known as Border, which can still be used for compatibility purposes)

Margin is optional and defaults to 0. Can be provided as a single dimension or as an object containing one or more of the following properties: 'top''left''bottom''right'. Margin values cannot be negative.

Format

Supported formats are: 'A3''A4''A5''Legal''Letter''Tabloid'.

Orientation

Orientation ('portrait''landscape') is optional and defaults to 'portrait'.

Headers and Footers

A repeating page header and footer can also be added via the header and footer property. These can be provided as an object that contains a height and a contents property. The contents property must be set as a phantom.callback object.

header: {height: "1.2cm",contents: phantom.callback(function(pageNum, numPages) {return "<h1>Header <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";})
}

Acceptable Objects

The given object should be in either this format:

{width: '200px',height: '300px',margin: '0px'
}

or this format:

{format: 'A4',orientation: 'portrait',margin: '1cm'
}

Examples

var webPage = require('webpage');
var page = webPage.create();page.paperSize = {width: '5in',height: '7in',margin: {top: '50px',left: '20px'}
};

A repeating page header and footer can also be added via this property, as in this example:

var webPage = require('webpage');
var page = webPage.create();page.paperSize = {width: '8.5in',height: '11in',header: {height: "1cm",contents: phantom.callback(function(pageNum, numPages) {return "<h1>Header <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";})},footer: {height: "1cm",contents: phantom.callback(function(pageNum, numPages) {return "<h1>Footer <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";})}
}

plainText

plainText {string}

Read-only. This property stores the content of the web page (main frame) as plain text — no element tags!

See also: page.content which returns the content with element tags.

Examples

var webPage = require('webpage');
var page = webPage.create();page.open('http://phantomjs.org', function (status) {console.log('Stripped down page text:\n' + page.plainText);phantom.exit();
});

scrollPosition

scrollPosition {object}

This property defines the scroll position of the web page.

Examples

var webPage = require('webpage');
var page = webPage.create();page.scrollPosition = {top: 100,left: 0
};

settings

settings {object}

This property stores various settings of the web page:

  • javascriptEnabled defines whether to execute the script in the page or not (defaults to true).
  • loadImages defines whether to load the inlined images or not (defaults to true).
  • localToRemoteUrlAccessEnabled defines whether local resource (e.g. from file) can access remote URLs or not (defaults to false).
  • userAgent defines the user agent sent to server when the web page requests resources.
  • userName sets the user name used for HTTP authentication.
  • password sets the password used for HTTP authentication.
  • XSSAuditingEnabled defines whether load requests should be monitored for cross-site scripting attempts (defaults to false).
  • webSecurityEnabled defines whether web security should be enabled or not (defaults to true).
  • resourceTimeout (in milli-secs) defines the timeout after which any resource requested will stop trying and proceed with other parts of the page. onResourceTimeout callback will be called on timeout.

Note: The settings apply only during the initial call to the page.open function. Subsequent modification of the settings object will not have any impact.

Examples

var webPage = require('webpage');
var page = webPage.create();
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';

title

Examples

page.open('http://phantomjs.org', function (status) {console.log(page.title); // get page Titlephantom.exit();
});

url

url {string}

Introduced: PhantomJS 1.7 Read-only. This property gets the current URL of the web page (main frame).

Examples

var webPage = require('webpage');
var page = webPage.create();page.open('http://phantomjs.org', function (status) {var url = page.url;console.log('URL: ' + url);phantom.exit();
});

viewportSize

viewportSize {object}

This property sets the size of the viewport for the layout process. It is useful to set the preferred initial size before loading the page, e.g. to choose between 'landscape' vs 'portrait'.

Because PhantomJS is headless (nothing is shown), viewportSize effectively simulates the size of the window like in a traditional browser.

Examples

var webPage = require('webpage');
var page = webPage.create();page.viewportSize = {width: 480,height: 800
};

You must include height for this to work.

var page = require('webpage').create(),address, output, size;
var system = require('system');	var arg_count = system.args.length - 1;
if (arg_count < 4 || arg_count > 5) {console.log('Usage: viewport.js URL filename sizeX sizeY');phantom.exit();
} else {address = system.args[1];output = system.args[2];sizeX = system.args[3];sizeY = system.args[4];page.viewportSize = { width: sizeX, height: sizeY };page.open(address, function (status) {if (status !== 'success') {console.log('Unable to load the address!');} else {window.setTimeout(function () {page.render(output);phantom.exit();}, 200);}});
}

Usage: viewport.js URL filename sizeX sizeY

windowName

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.windowName example.

zoomFactor

zoomFactor {number}

This property specifies the scaling factor for the page.render and page.renderBase64functions.

The default is 1, i.e. 100% zoom.

Examples

Create a thumbnail preview with 25% zoom

var webPage = require('webpage');
var page = webPage.create();page.zoomFactor = 0.25;
page.render('capture.png');

这篇关于phontomjs webpage模块属性的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/966031

相关文章

python: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空

深入探索协同过滤:从原理到推荐模块案例

文章目录 前言一、协同过滤1. 基于用户的协同过滤(UserCF)2. 基于物品的协同过滤(ItemCF)3. 相似度计算方法 二、相似度计算方法1. 欧氏距离2. 皮尔逊相关系数3. 杰卡德相似系数4. 余弦相似度 三、推荐模块案例1.基于文章的协同过滤推荐功能2.基于用户的协同过滤推荐功能 前言     在信息过载的时代,推荐系统成为连接用户与内容的桥梁。本文聚焦于

滚雪球学Java(87):Java事务处理:JDBC的ACID属性与实战技巧!真有两下子!

咦咦咦,各位小可爱,我是你们的好伙伴——bug菌,今天又来给大家普及Java SE啦,别躲起来啊,听我讲干货还不快点赞,赞多了我就有动力讲得更嗨啦!所以呀,养成先点赞后阅读的好习惯,别被干货淹没了哦~ 🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,助你一臂之力,带你早日登顶🚀,欢迎大家关注&&收藏!持续更新中,up!up!up!! 环境说明:Windows 10

Jenkins构建Maven聚合工程,指定构建子模块

一、设置单独编译构建子模块 配置: 1、Root POM指向父pom.xml 2、Goals and options指定构建模块的参数: mvn -pl project1/project1-son -am clean package 单独构建project1-son项目以及它所依赖的其它项目。 说明: mvn clean package -pl 父级模块名/子模块名 -am参数

寻迹模块TCRT5000的应用原理和功能实现(基于STM32)

目录 概述 1 认识TCRT5000 1.1 模块介绍 1.2 电气特性 2 系统应用 2.1 系统架构 2.2 STM32Cube创建工程 3 功能实现 3.1 代码实现 3.2 源代码文件 4 功能测试 4.1 检测黑线状态 4.2 未检测黑线状态 概述 本文主要介绍TCRT5000模块的使用原理,包括该模块的硬件实现方式,电路实现原理,还使用STM32类

HTML5自定义属性对象Dataset

原文转自HTML5自定义属性对象Dataset简介 一、html5 自定义属性介绍 之前翻译的“你必须知道的28个HTML5特征、窍门和技术”一文中对于HTML5中自定义合法属性data-已经做过些介绍,就是在HTML5中我们可以使用data-前缀设置我们需要的自定义属性,来进行一些数据的存放,例如我们要在一个文字按钮上存放相对应的id: <a href="javascript:" d

Python中的属性装饰器:解锁更优雅的编程之道

引言 在Python的世界里,装饰器是一个强大的工具,它允许我们以一种非侵入性的方式修改函数或方法的行为。而当我们谈论“属性装饰器”时,则是在探讨如何使用装饰器来增强类中属性的功能。这不仅让我们的代码更加简洁、易读,同时也提供了强大的功能扩展能力。本文将带你深入了解属性装饰器的核心概念,并通过一系列实例展示其在不同场景下的应用,从基础到进阶,再到实际项目的实战经验分享,帮助你解锁Python编程

python内置模块datetime.time类详细介绍

​​​​​​​Python的datetime模块是一个强大的日期和时间处理库,它提供了多个类来处理日期和时间。主要包括几个功能类datetime.date、datetime.time、datetime.datetime、datetime.timedelta,datetime.timezone等。 ----------动动小手,非常感谢各位的点赞收藏和关注。----------- 使用datet

C8T6超绝模块--EXTI

C8T6超绝模块–EXTI 大纲 控制流程结构体分析EXTI实现按键 具体案例 控制流程 这里是流程框图,具体可以去看我STM32专栏的EXTI的具体分析 结构体分析 typedef struct {uint32_t EXTI_Line; // 中断/事件线EXTIMode_TypeDef EXTI_Mode; // EXTI 模式EXTITrigger_TypeDef EXTI_

1、创建多模块的maven springboot项目

现在的java的项目都是多模块的,这次也跟个风。 目标:实现下述结构 项目AcedBoot, 子模块:         aced-api 对外提供接口,         aced-web 给前端提供接口,         aced-service 服务层,         aced-dao 数据底层,包含数据库mapper和实体类entity,         aced-commo