K
K
kochkaqqq2020-09-04 21:01:50
JavaScript
kochkaqqq, 2020-09-04 21:01:50

Why doesn't it output html when parsing a web page, but outputs an object??

Here is an example:

const request = require('request-promise');
const cheerio = require('cheerio');

const options = {
    url: 'https://www.php.net/manual/ru/function.get-browser.php',
    headers: {
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
    }
  };

  function callback (error, response, html) {
      if (!error && response.statusCode == 200) {
          return html;
      } else {
          console.log("error has occured");
          console.log(error);
      }
  }

module.exports = function webData () {  
    try {
        const page = request.get(options, callback);
        var $ = cheerio.load(page);
        var h1 = $('h1.refname');
        console.log(h1);
    } catch(err) {
        console.log(err);
    }   
}

Result:

initialize {
options: {
withDomLvl1: true,
normalizeWhitespace: false,
xml: false,
decodeEntities: true
},
_root: initialize {
'0': {
type: 'root',
name: 'root',
namespace: ' www. w3.org/1999/xhtml ',
attribs: [Object: null prototype] {},
'x-attribsNamespace': [Object: null prototype] {},
'x-attribsPrefix': [Object: null prototype] {},
children: [Array],
parent: null,
prev: null,
next: null
},
options:{
withDomLvl1: true,
normalizeWhitespace: false,
xml: false,
decodeEntities: true
},
length: 1,
_root: [Circular]
},
length: 0,
prevObject: initialize {
'0': {
type: 'root',
name: 'root',
namespace : ' www.w3.org/1999/xhtml ',
attribs: [Object: null prototype] {},
'x-attribsNamespace': [Object: null prototype] {},
'x-attribsPrefix': [Object: null prototype ] {},
children: [Array],
parent: null,
prev: null,
next: null
},
options: {
withDomLvl1: true,
normalizeWhitespace: false,
xml: false,
decodeEntities: true
},
length: 1,
_root: [Circular]
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2020-09-06
@SilenceOfWinter

because $('h1.refname') returns a jQuery object. use something like $('h1.refname').get(0).innerHTML

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question