gpt4 book ai didi

javascript - PhantomJS 迭代 HTML 页面元素

转载 作者:行者123 更新时间:2023-12-03 05:48:14 24 4
gpt4 key购买 nike

有人可以帮我解决这个问题吗?

我正在使用 PhantomJS 抓取本地 HTML 文件,并尝试在屏幕上显示“test”类的 HTML 标签的内容。我确实获得了第一个标签的内容,但不知何故我没有获得下一个标签。

-= HTML/index.html =-

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="parent-id">
<p>hello word 1</p>
<p class="test">hello word 2</p>
<p class="test">hello word 3</p>
<p>hello word 4</p>
</div>
</body>
</html>

-= PhantomJS/test2.js =-

var fs = require('fs');
var page = require('webpage').create();

page.settings.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:49.0) Gecko/20100101 Firefox/49.0';
page.viewportSize = {width:1200, height:1024};

page.onConsoleMessage = function (msg) {
//console.log(msg);
}

page.open('http://localhost/index.html', function(status) {
if (status == 'success') {
var products = page.evaluate(function() {
return document.getElementsByClassName('test')
});

for(var i = 0; i < products.length; ++i) {
if(products[i]) {
console.log(products[i].innerHTML);
}
}

phantom.exit();
} else {
console.log('Unable to load the address!');
phantom.exit();
}
});

当我运行 phantomjs "test2.js"时,我得到:

hello word 2

虽然我希望得到:

hello word 2
hello word 3

最佳答案

您不应从页面上下文中返回元素。返回简单值。此示例适用于您的index.html:

page.open('http://localhost/index.html', function(status) {
if (status == 'success') {
var products = page.evaluate(function() {
return [].map.call(document.getElementsByClassName('test'), function(elem) {
return elem.innerHTML;
});
});

for(var i = 0; i < products.length; ++i) {
if(products[i]) {
console.log(products[i]);
}
}

phantom.exit();
} else {
console.log('Unable to load the address!');
phantom.exit();
}
});

关于javascript - PhantomJS 迭代 HTML 页面元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40247354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com