const cheerio = require('cheerio'); const axios = require('axios'); // 堆代码 duidaima.com (async () => { const response = await axios.get('https://www.example.com'); const $ = cheerio.load(response.data); const title = $('title').text(); const content = $('body').text(); console.log('Title:', title); console.log('Content:', content); })();示例二:抓取列表项
const cheerio = require('cheerio'); const axios = require('axios'); (async () => { const response = await axios.get('https://www.example.com/products'); const $ = cheerio.load(response.data); const products = []; $('div.product').each((index, element) => { const product = { name: $(element).find('h2').text(), price: $(element).find('.price').text(), description: $(element).find('p.description').text() }; products.push(product); }); console.log(products); })();示例三:处理分页
const cheerio = require('cheerio'); const axios = require('axios'); (async () => { let page = 1; const maxPages = 5; const allProducts = []; while (page <= maxPages) { const response = await axios.get(`https://www.example.com/products?page=${page}`); const $ = cheerio.load(response.data); $('div.product').each((index, element) => { const product = { name: $(element).find('h2').text(), price: $(element).find('.price').text(), description: $(element).find('p.description').text() }; allProducts.push(product); }); page++; } console.log(allProducts); })();优点
5.与其他库的兼容性:Cheerio可以轻松集成其他Node.js库(如Axios),创建更全面的网络爬虫解决方案。