我目前正在构建一个爬虫,根据我想要的关键字(例如“环境”和“气候”)从报纸网站上抓取数据。但是最近我遇到了一个网站,无法简单地检查文本是否包含我想要的关键字。它的 HTML 是这样写的:
<a aria-label="Ravaged by floods, Bangladesh pitches plan to adapt to climate impacts" class="card-with-image-zoom" href="https://en.prothomalo.com/environment/climate-change/ravaged-by-floods-bangladesh-pitches-plan-to-adapt-to-climate-impacts">
以下是我使用Cheerio查找关键词的方法:
const html = response.data;
const $ = cheerio.load(html);
const specificarticles = [];
$('a:contains("climate"),a:contains("environment")',HTML)
如何使用Cheerio检查此特定HTML中关键字的可用性?
.getAttribute()
方法来获取<a>
标签中aria-label
的值。然后检查该字符串是否包含关键词。如果是,你需要将文章的超链接存储起来。 - Shilly