Can I use XPath selectors in Cheerio?

No, you can not use XPath selectors in
Cheerio
. According to
these
GitHub issues
, there is no plan to support XPaths in Cheerio.

However, if you simply want to work with XML documents and parse those using Cheerio, it is possible. Here is some sample code for parsing XML using Cheerio.

const cheerio = require('cheerio');
const xml = `
  
    
      Practical Python Projects
      Yasoob Khalid
      2022
      39.95
    
    
      Intermediate Python
      Yasoob Khalid
      2018
      29.99
    
  
`;

// Load the XML document as a Cheerio object
const $ = cheerio.load(xml, { xml: true });

// Select all book titles 
const titles = $('book > title');

// Print the text content of each title
titles.each((i, title) => {
    console.log($(title).text());
});

// Output:
// Practical Python Projects
// Intermediate Python

Related Cheerio web scraping questions:

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *