phontomjs webpage模块属性

article/2025/10/28 20:46:20

随时随地技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(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');


http://chatgpt.dhexx.cn/article/aebllBR6.shtml

相关文章

phontomjs webPage模块方法

随时随地阅读更多技术实战干货&#xff0c;获取项目源码、学习资料&#xff0c;请关注源代码社区公众号(ydmsq666) addCookie addCookie(Cookie) {boolean} Introduced: PhantomJS 1.7 Add a Cookie to the page. If the domain does not match the current page, the Cooki…

webpage基本使用

webpack的基本使用 一.安装webpack (1) 打开项目目录终端&#xff0c;输入命令: npm install webpack webpack-cli -D (2) 然后在项目根目录中&#xff0c;创建一个 webpack.config.js 的配置文件用来配置webpack 在 webpack.config.js 文件中编写代码进行webpack配置&#…

集电极开路的门电路 OC门

当几个与非门输出端用导线直接相连时&#xff0c;可能会出现几个门的电流冲到同一个二极管上&#xff0c;造成烧坏&#xff0c;所以用集电极开路的门电路&#xff0c;统一在输出端接外接电源。 单个门电路的电路图 符号 使用时&#xff0c;一般将几个输出段导线连接&#xff0c…

OC门电路

灰度变换处理方法 什么是OC门 即集电极开路门电路&#xff0c;OD门&#xff0c;即漏极开路门电路&#xff0c;必须外界上拉电阻和电源才能将开关电平作为高低电平用。否则它一般只作为开关大电压和大电流负载&#xff0c;所以又叫做驱动门电路。 oc门电路工作原理  实际使用中…

一文搞懂OC门、OD门及其作用

我们先给出OC门、OD门的定义&#xff0c;然后从原理出发&#xff0c;介绍OC门、OD门的作用。 1 什么是OC门、OD门 OC门&#xff08;Open Collector Gate&#xff09;&#xff1a;集电极开路门&#xff0c;如图1所示&#xff0c;当N1导通时&#xff0c;输出低电平&#xff1b;当…

TTL反相器、OC门、TS门、推挽输出、开漏输出

TTL反相器 这是一个TTL反相器&#xff0c;这是经过了很多工程师多种设计最终沉淀出来的电路&#xff0c;这个电路是比较成熟的。我们只需要对这个电路进行解析即可&#xff0c;不需要再去研究当初是如何设计出来的。 学过CMOS应该知道&#xff0c;右侧的输出级其实也是个推挽输…

理解OC门

注&#xff1a;本文转载自新浪博客。 什么是集电极开路&#xff08;OC&#xff09;&#xff1f; 我们先来说说集电极开路输出的结构。集电极开路输出的结构如图1所示&#xff0c;右边的那个三极管集电极什么都不接&#xff0c;所以叫做集电极开路&#xff08;左边的三极管为反相…

OC门

reference: http://www.cnblogs.com/xiangxiangyuan/p/3787831.html (其他总结也精彩&#xff09; OC&#xff08;open collector)门&#xff0c;又称 集电极开路门。 OD门&#xff08;Open Drain&#xff0c; 漏极开路门&#xff0c;对场效应管而言&#xff09;。实际使用中&a…

Golang 切片

文章目录 和数组的区别声明空切片可以和nil相等切片长度,容量切片增加元素切片删除元素切片排序切片截取范围多维切片 和数组的区别 1.数组是定长,切片不是 2.数组是值传递&#xff0c;切片不是 声明 1.通过var声明 2.简短声明 package mainimport "fmt"func mai…

Java 各种 切片 操作(Array String List)

在python的 [xx:xx] 切片操作中快乐之后&#xff0c;返回Java变成发现这个习惯保持保持下去了&#xff01;&#xff0c;可惜JDK并不认账反手给我爆了两个红JDK&#xff1a;这是别人的香水味&#xff01; 所以这次总结一下在Java中各种数据类型的切片操作如何实现&#x…

python切片操作方法小总结

文章参考自&#xff1a;link. python切片的详细总结 最近学习python切片&#xff0c;时而醍醐灌顶时而云里雾里&#xff0c;今天终于弄清楚了 python切片 里面自己经常疑惑的地方&#xff0c;记录一下&#xff0c;方便以后查看。 一、切片的2种索引方向 如果掌握切片方法&a…

Python中numpy数组切片

参考Python中numpy数组切片 - 云社区 - 腾讯云 1、基本概念 Python中符合切片并且常用的有&#xff1a;列表&#xff0c;字符串&#xff0c;元组。 下面那列表来说明&#xff0c;其他的也是一样的。 格式&#xff1a;[开头&#xff1a;结束&#xff1a;步长] 开头&#xff1a…

python切片

前言 所谓切片就是在某个数据里提取需要的部分&#xff0c;提取到的是某个索引下的值&#xff0c;或者索引区间的值 一、认识切片 ① 支持切片操作的数据类型有&#xff1a; list&#xff08;列表&#xff09;&#xff0c;tuple&#xff08;元祖&#xff09;&#xff0c;st…

字符串的切片操作

字符串的切片操作&#xff1a; 1.知识点&#xff1a; str[start: end:方向和步长]&#xff0c;没有方向和步长默认正向。 方向&#xff1a; 1 表示从左向右 0,1,2,3,4&#xff0c;5。。。 -1 表示从右向左 注意数值的顺序&#xff1a;[5:0]正向不行&#xff0c;反向行 [0:5]正向…

25. Python 字符串的切片方法

25. Python 字符串的切片方法 文章目录 25. Python 字符串的切片方法1. 索引取值知识回顾2. 什么是切片3. 切片的语法4. 切片示例4.1 步长为1的切片取值4.2 步长为2的切片取值4.3 省略开始索引4.4 省略结束索引4.5 步长为-2 5. 课堂练习6. 总结 1. 索引取值知识回顾 【索引取值…

Go语言切片详解

切片&#xff08;slice&#xff09;是对数组的一个连续片段的引用&#xff0c;所以切片是一个引用类型&#xff08;因此更类似于 C/C 中的数组类型&#xff0c;或者 Python 中的 list 类型&#xff09;&#xff0c;这个片段可以是整个数组&#xff0c;也可以是由起始和终止索引…

字符串的切片1

切片的基本语法格式&#xff1a;[头下标&#xff1a;尾下标&#xff1a;步长] 1.字符串切片可以对字符串进行截取操作&#xff0c;获取一段子串。 2.[头下标&#xff1a;尾下标]遵循前闭后开规则&#xff08;头标可取到&#xff0c;尾标取不到&#xff09;。 3.步长可省略&a…

Python3的切片

Python3的切片非常灵活&#xff0c;可以很方便地对有序序列进行切片操作&#xff0c;代码简单易用。 大家都知道&#xff0c;我们可以直接使用索引来访问序列中的元素&#xff0c;同时索引可分为正向和负向两种&#xff0c;而切片也会用到索引&#xff0c;下面放上一个图&…

Python列表切片详解([][:][::])

Python切片是list的一项基本的功能&#xff0c;最近看到了一个程序&#xff0c;里面有这样一句类似的代码&#xff1a; a list[::10] 不太明白两个冒号的意思就上网百度&#xff0c;发现大多数人写的博客中都没有提到这一个用法&#xff0c;因此自己翻阅《Python核心编程中文…

Numpy 数组切片

一、列表切片&#xff08;一维数组&#xff09; 1.1、切片原理 列表切片是从原始列表中提取列表的一部分的过程。在列表切片中&#xff0c;我们将根据所需内容&#xff08;如&#xff0c;从何处开始&#xff0c;结束以及增量进行切片&#xff09;剪切列表。Python中符合序列的…