How to use the load function from cheerio

Find comprehensive JavaScript cheerio.load code examples handpicked from public code repositorys.

cheerio.load is a method that creates a jQuery-style DOM manipulator for a given HTML string or DOM.

66
67
68
69
70
71
72
73
74
75
if ('development' === wpMode) {
    // Locate all E2E cases
    const caseFiles = [];
    fs.readdirSync(path.resolve(__dirname, 'test/html/')).forEach(file => {
        const absPath = path.resolve(__dirname, 'test/html/', file);
        const caseHtml = cheerio.load(fs.readFileSync(absPath));
        const publicName = file.replace('.tpl', '');

        plugins.push(new HtmlWebpackPlugin({
            template: path.resolve(__dirname, 'test/html/', file),
fork icon316
star icon673
watch icon194

+ 5 other calls in file

176
177
178
179
180
181
182
183
184
185
  })
  return liParents.reverse()
}

_buildErrorPage(errorFrameHtml) {
  let $ = cheerio.load(errorFrameHtml, { decodeEntities: false })
  let p404 = {
    title: "页面未找到 - " + this.config.doc.name,
    path: this.buildDir + "/teadocs_error_pages/404.html",
    description: "404错误!该页面未找到,试试从左边菜单栏访问别的页面,或者从左上角搜索你想要的内容。"
fork icon29
star icon157
watch icon6

+ 7 other calls in file

How does cheerio.load work?

cheerio.load() is a function in the Cheerio library that loads an HTML string into a jQuery-like DOM representation, allowing users to manipulate and traverse the DOM using familiar jQuery syntax. When invoked, it returns a Cheerio instance which can be used to perform various operations on the HTML, such as selecting and manipulating DOM elements, querying for attributes or content, and more.

179
180
181
182
183
184
185
186
187
188
extractor.prototype.extract = function(crawl_info){
    if(crawl_info['origin']['format']=='binary')return crawl_info;
    var extract_rule = this.spiderCore.spider.getDrillerRule(crawl_info['origin']['urllib'],'extract_rule');

    if(crawl_info['origin']['drill_rules']||extract_rule['rule']){
        var $ = cheerio.load(crawl_info['content']);
    }

    if(crawl_info['origin']['drill_rules']){
        if(crawl_info['drill_link']){
fork icon54
star icon151
watch icon16

+ 3 other calls in file

17
18
19
20
21
22
23
24
25
26
  htmlFiles.forEach(processFile);
}

function processFile(path) {
  const originalText = fs.readFileSync(path, 'utf8');
  let $ = cheerio.load(originalText);

  hideExternals($);
  if (process.env.CDN_ROOT) {
    addCdnPrefix($, process.env.CDN_ROOT);
fork icon21
star icon25
watch icon12

+ 3 other calls in file

Ai Example

1
2
3
4
const cheerio = require("cheerio");
const html = " Hello, world! ";
const $ = cheerio.load(html);
console.log($("h1").text()); // Output: "Hello, world!"

In this example, we require the cheerio library and create a string variable html containing some HTML content. We then use cheerio.load(html) to create a Cheerio object, $, from the HTML string. Finally, we use $ to select the h1 element and log its text content to the console.

25
26
27
28
29
30
31
32
33
34
}

async start() {
  request(this.url, async (error, res, body) => {
    if (!error && res.statusCode == 200) {
      const $ = cheerio.load(body)
      const $cardBlock = $('.panel')

      for (let i = 0; i < $cardBlock.length; i++) {
        const secondCategoryName = $('.panel-title.card').eq(i).text().trim()
fork icon424
star icon0
watch icon37

+ 5 other calls in file

210
211
212
213
214
215
216
217
218
  }
}

function load (html, url) {
  html = html || ''
  var $ = html.html ? html : cheerio.load(html, { decodeEntities: false })
  if (url) $ = absolutes(url, $)
  return $
}
fork icon372
star icon0
watch icon110

+ 5 other calls in file

13
14
15
16
17
18
19
20
21
22
fs.writeFileSync(path.join(rootDir, 'src', 'index.ts'), '', 'utf-8')

icons.forEach((i) => {
  const svg = fs.readFileSync(i, 'utf-8')
  const id = path.basename(i, '.svg')
  const $ = cheerio.load(svg, {
    xmlMode: true,
  })
  const fileName = path.basename(i).replace('.svg', '.tsx')
  const location = path.join(rootDir, 'src/icons', fileName)
fork icon241
star icon0
watch icon27

336
337
338
339
340
341
342
343
344
345
346
347
    }
    return html;
}


function start(bookIns, page) {
    var $ = cheerio.load(page.content);
    var modifyHeader = !/<!--[ \t]*ex_nolevel[ \t]*-->/.test(page.content)


    // 处理toc相关,同时处理标题和id
    var tocs = handlerTocs($, page, modifyHeader);
fork icon10
star icon23
watch icon1

+ 9 other calls in file

12
13
14
15
16
17
18
19
20
21
const response = await got({
    method: 'get',
    url: currentUrl,
});

const $ = cheerio.load(response.data);

let items = $('a.question-title')
    .slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 25)
    .toArray()
fork icon0
star icon15
watch icon1

+ 3 other calls in file

97
98
99
100
101
102
103
104
105
106
  output: "HTML",
};
var sn = truecallerjs.searchNumber(searchData);
sn.then(async function (response) {
  //console.log(response);
  const $ = cheerio.load(response);
  let name = "";
  $("td").each(function (i, el) {
    if ($(this).text() === "name") {
      if ($(this).next().text() !== "") {
fork icon1
star icon2
watch icon1

+ 4 other calls in file

1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
html = html.replace('objectDefaultFiles/envelope.js', level + 'objectDefaultFiles/envelope.js');
html = html.replace('objectDefaultFiles/envelopeContents.js', level + 'objectDefaultFiles/envelopeContents.js');

html = html.replace('objectDefaultFiles/gl-worker.js', level + 'objectDefaultFiles/gl-worker.js');

var loadedHtml = cheerio.load(html);
var scriptNode = '<script src="' + level + 'objectDefaultFiles/object.js"></script>';
scriptNode += '<script src="' + level + 'objectDefaultFiles/pep.min.js"></script>';

// inject the server IP address, but don't inject the objectKey and frameKey, as those come from the editor
fork icon14
star icon19
watch icon4

+ 9 other calls in file

13
14
15
16
17
18
19
20
21
22
if (error) {
  return console.error(error);
}

if (!error && res.statusCode == 200) {
  const $ = cheerio.load(body);
  $(".results-name").each(function (i, plugin) {
    if (typeof (plugin.parent.parent.children[1].children[1].children[1]) !== "undefined") {
      var pluginn = {
        name: plugin.children[1].children[0].data,
fork icon10
star icon19
watch icon3

+ 5 other calls in file

345
346
347
348
349
350
351
352
353
354
355
    return file;
};


const builtToc = (html) => {
    const toc = {};
    const $ = cheerio.load(html);
    $('h1, h2, h3, h4, h5, h6').map((_, element) => {
        if(!toc[element.name]){
            toc[element.name] = [];
        }
fork icon7
star icon46
watch icon3

+ 4 other calls in file

4
5
6
7
8
9
10
11
12
13
14
15
	return input.replace(/\s{2,}/g, "").replace(/\'/g, '"')
}


module.exports = async (value, outputPath) => {
	if (outputPath && outputPath.endsWith(".html")) {
		const $ = cheerio.load(value)


		// We have to process headings from table of contents before adding
		// the permalink to them later, or the links will include the text of
		// the permalink.
fork icon3
star icon13
watch icon0

415
416
417
418
419
420
421
422
423
424
    dataTotalTests += (todayData.tests || todayData.positives);
}

return request(reportURL).then(reportData => {

    const html = cheerio.load(reportData.replace(/[\n]/g, ''));

    const text = html('.Page-document p').text();

    function parseReportNumber(text, regExp) {
fork icon1
star icon7
watch icon1

+ 4 other calls in file

16
17
18
19
20
21
22
23
24
25
function putIntoTemplate(page, templatePath) {
	if (!templateHTML) {
		templateHTML = loadTemplate(templatePath);
	}
	
	var $template = cheerio.load(templateHTML, cheerioOptions);
	var $ = cheerioLoader.load(page);
	var title = $("h1").text();
	var $primaryTarget = $template("content");
	var $secondaryTarget = $template(".container");
fork icon3
star icon6
watch icon5

20
21
22
23
24
25
26
27
28
29
30
  const hasBody = /<\s*body(\w|\s|=|"|-)*>/gm;
  return hasBody.test(content);
}


async function replaceSpecialLinks(content, options) {
  const $ = cheerio.load(content);
  // TODO: only block links
  const replace = ['bookmark', 'embed','textTweet'];
  let links = $("a").filter((i, el) => {
    const text = $(el).text();
fork icon1
star icon3
watch icon2

46
47
48
49
50
51
52
53
54
55
	removeScriptTypeAttributes: true
},
parsers: {
	get_kids: (html, callback = () => {}) => {
		html = minify(html, this.Parsing_Interface.minify_options);
		let cheerio2 = cheerio.load(html);
		let kids = [];
		cheerio2('.pupil-selector .form-group .form-control').each((i, elem) => {
			kids.push({ name: cheerio2(elem).text(), id: cheerio2(elem).val() });
		});
fork icon0
star icon4
watch icon2

+ 131 other calls in file

534
535
536
537
538
539
540
541
542
543
try {
	const options = {
		timeout: 5000,
	};
	const { payload } = await Wreck.get(url, options);
	const $ = Cheerio.load(payload.toString());
	const title = $('title').text();
	return title;
} catch (err) {
	if (err.isBoom && err.output.statusCode === 504) {
fork icon0
star icon3
watch icon1

+ 3 other calls in file

1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
    })
}


async function mediafire(url) {
let query = await axios.get(url) 
let cher = cheerio.load(query.data)
let hasil = []
let link = cher('a#downloadButton').attr('href')
let size = cher('a#downloadButton').text().replace('Download', '').replace('(', '').replace(')', '').replace('\n', '').replace('\n', '').replace(' ', '')
let seplit = link.split('/')
fork icon1
star icon0
watch icon1

+ 359 other calls in file