乐闻世界logo
搜索文章和话题

How to extract uppercased attributes with Cheerio

1个答案

1

When using Cheerio to extract uppercase attributes from HTML elements, it's important to note that Cheerio is built on jQuery and is typically case-insensitive for attribute names. Specifically, Cheerio converts attribute names to lowercase uniformly. Consequently, directly accessing uppercase attribute names may not yield the expected results. However, you can access the original attributes of an element—including their case sensitivity—through Cheerio's attribs property.

Here is an example demonstrating how to use Cheerio to extract elements with uppercase attributes:

Suppose we have the following HTML content:

html
<html> <head></head> <body> <div id="example" DATA-ATTR="some value"></div> </body> </html>

We need to extract the DATA-ATTR attribute from the div element. Below is a code example illustrating how to achieve this with Cheerio:

javascript
const cheerio = require('cheerio'); const html = `\n<html>\n<head></head>\n<body>\n <div id="example" DATA-ATTR="some value"></div>\n</body>\n</html>\n`; // Load HTML string into Cheerio const $ = cheerio.load(html); // Select the specific element const element = $('#example'); // Access the original attributes directly via the .attribs property const dataAttr = element[0].attribs['DATA-ATTR']; console.log(dataAttr); // Output: some value

In this example, we first load the HTML content into Cheerio. Next, we use a selector to find the div element with id 'example'. Since Cheerio internally converts attribute names to lowercase, we access the element's attribs property, which is an object containing all original attributes (including their case sensitivity). By directly referencing the uppercase attribute name DATA-ATTR, we successfully extract the attribute value some value.

This approach is effective for handling any case-sensitive attributes in HTML elements and is particularly useful when dealing with non-standard or special HTML markup.

2024年8月10日 01:11 回复

你的答案