我正在尝试使用 Cheerio 在 NodeJS 中使用 Phantom 从 html 中提取 div 元素
I'm trying to use Cheerio extract a div element from an html using Phantom in NodeJS
我正在尝试仅提取 class 名称中的元素 pod pod--rounded
。
我想要的元素在 this site
的 'My Listings' 部分
可以找到 Phantom NPM 包 here。
var phantom = require("phantom");
const cheerio = require('cheerio')
var _ph, _page, _outObj;
phantom.create().then(function(ph){
_ph = ph;
return _ph.createPage();
}).then(function(page){
_page = page;
return _page.open('https://www.bhgre.com/Better-Homes-and-Gardens-Real-Estate-Big-Hill-5569c/Suzan-Jackson-300497a');
}).then(function(status){
console.log(status);
return _page.property('content')
}).then(function(content){
const $ = cheerio.load(content)
console.log($('div, pod pod--rounded'));
_page.close();
_ph.exit();
}).catch(function(e){
console.log(e);
});
如果控制台日志只是 content
,则返回整个 html。我想我无法理解 Cheerio 的逻辑。当我尝试使用 Cheerio 解析东西时,我得到的东西看起来部分像这样......
'x-attribsPrefix': [Object: null prototype] {},
children: [ [Object], [Object], [Object] ],
parent:
{ type: 'tag',
name: 'td',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object],
'x-attribsNamespace': [Object],
'x-attribsPrefix': [Object],
children: [Array],
parent: [Object],
prev: null,
...
这个对象是什么?这是JSON吗?
如果您要在浏览器中转到 site、'inspect' 我的列表元素,请右键单击 div pod pod--rounded
并复制元素正是我希望 Node 从页面中提取的内容。 div.
中的所有 html
我认为您的选择器应该从 div, pod pod--rounded
更改为 div.pod.pod--rounded
。并根据 cheerio's documents:
Cheerio collections are made up of objects that bear some resemblence to browser-based DOM nodes.
我正在尝试仅提取 class 名称中的元素 pod pod--rounded
。
我想要的元素在 this site
可以找到 Phantom NPM 包 here。
var phantom = require("phantom");
const cheerio = require('cheerio')
var _ph, _page, _outObj;
phantom.create().then(function(ph){
_ph = ph;
return _ph.createPage();
}).then(function(page){
_page = page;
return _page.open('https://www.bhgre.com/Better-Homes-and-Gardens-Real-Estate-Big-Hill-5569c/Suzan-Jackson-300497a');
}).then(function(status){
console.log(status);
return _page.property('content')
}).then(function(content){
const $ = cheerio.load(content)
console.log($('div, pod pod--rounded'));
_page.close();
_ph.exit();
}).catch(function(e){
console.log(e);
});
如果控制台日志只是 content
,则返回整个 html。我想我无法理解 Cheerio 的逻辑。当我尝试使用 Cheerio 解析东西时,我得到的东西看起来部分像这样......
'x-attribsPrefix': [Object: null prototype] {},
children: [ [Object], [Object], [Object] ],
parent:
{ type: 'tag',
name: 'td',
namespace: 'http://www.w3.org/1999/xhtml',
attribs: [Object],
'x-attribsNamespace': [Object],
'x-attribsPrefix': [Object],
children: [Array],
parent: [Object],
prev: null,
...
这个对象是什么?这是JSON吗?
如果您要在浏览器中转到 site、'inspect' 我的列表元素,请右键单击 div pod pod--rounded
并复制元素正是我希望 Node 从页面中提取的内容。 div.
我认为您的选择器应该从 div, pod pod--rounded
更改为 div.pod.pod--rounded
。并根据 cheerio's documents:
Cheerio collections are made up of objects that bear some resemblence to browser-based DOM nodes.