简体   繁体   English

如何使用 Cheerio 检查多个关键字?

[英]How do i check for multiple keywords using cheerio?

Currently, I am trying to build a scrapper that searches all <a> tags for specific keywords like "climate" or "environment."目前,我正在尝试构建一个抓取器,用于搜索所有<a>标记以查找特定关键字,例如“气候”或“环境”。 Using cheerio, is it possible to look for multiple keywords so that I get results of multiple keywords?使用cheerio,是否可以查找多个关键字以便获得多个关键字的结果?

Here is my code-这是我的代码-

const PORT = 8000;
const express = require('express');
const axios = require('axios');
const cheerio = require('cheerio');
const { response } = require('express');

const app = express();

const articles = [];

app.get('/',(req,res)=>{
    res.json('Hello World')
})

app.get('/news',(req,res)=>{
    axios.get('https://www.tbsnews.net/bangladesh/environment/climate-change')
        .then((response)=>{
            const html = response.data;
            const $ = cheerio.load(html);

            $('a:contains("climate")',html).each(function(){
                const title = $(this).text()
                const url = $(this).attr('href')
                articles.push({
                    title,
                    url
                })
            })
            res.json(articles)
        }).catch((err)=>console.log(err));
})

app.listen(PORT,()=>{console.log(`server running on Port ${PORT}`)});

From the documentation of cheerio, you can use multiple contains selectors similarly you would use them in jQuery.从cheerio 的文档中,您可以使用多个contains选择器,就像在 jQuery 中使用它们一样。

Logical OR逻辑或

If you need to match any of the words, just separate the contains selectors with a comma.如果您需要匹配任何单词,只需用逗号分隔contains选择器。

$('a:contains("climate"), a:contains("environment")', html)

Logical AND逻辑与

If you need to match exactly the two words, add the second contains selector right after the first.如果您需要完全匹配这两个词,请在第一个之后添加第二个contains选择器。

$('a:contains("climate"):contains("environment")', html)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM