So I am scraping a website which has a DOM something similar to that:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p style='color: #ff9900'></p>
<p></p>
<p></p>
<p style='color: #ff0000'>2</p>
<p></p>
<p></p>
<p style='color: #ff9900'>3</p>
<p></p>
<p style='color: #ffffff'>4</p>
<p></p>
</body>
</html>
As you can see, there are <p>
tags that have style attribute. I would like to get the elements only containing the style attribute.
const $ = cheerio.load(page, {
normalizeWhitespace: true,
xmlMode: false
});
const item = [];
$('p:style="color:#ff9900').each(function(){
item.push($(this).text())
})
console.log(item)
I want to know if there is any chance to have a style as a selector in cheerio.
There is a chance to have it as such p[style]
, but it will return every element that have the attribute like this. Let's say I want it to return elements with only certain style style="color:#ff9900
not style="color:#ffffff