如何使用cheerio或puppeteer抓取所有href?

我有一种情况,需要从网站获取所有社交媒体链接。如果我认为所有社交链接都在首页上,那么每个社交链接都将获取每个社交媒体链接。以下是代码示例:

使用cheeriojs

const cheerio = require('cheerio')
const axios = require('axios')
const https = require('https');

const agent = new https.Agent({
    rejectUnauthorized: false
});
// process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

const getLinks = async (url) => {

    try {
        let body = await axios.get(url, { httpsAgent: agent })
        let hrefs = []
        let $ = cheerio.load(body.data)
        let links = $('a')

        links.each((i, link) => {
            hrefs.push($(link).attr('href'))
        })
        return hrefs
    } catch (error) {
        return error
    }

}

const getSocialLinks = async (socialLinks, url) => {
    try {
        let hrefs = await getLinks(url)

        let handles = []
        hrefs.filter(href => {
            if (href) {
                for (const link of socialLinks) {
                    if (href.includes(link)) {
                        handles.push({ platform: link, handle: href })
                        break
                    }
                }
            }
        })

        console.log(handles);
    } catch (error) {
        console.log(error)
    }
}

getSocialLinks(['facebook', 'twitter', 'instagram', 'youtube', 'linkedin'], 'https://synavos.com')
如果所有社交媒体链接都在主页上,则效果很好,但是   我无法弄清楚社交媒体链接是否打开   给定网站的其他页面。

以下是使用puppeteer的相同代码示例:

const puppeteer = require('puppeteer')

const getHrefsAttributes = async (website) => {
    try {
        const browser = await puppeteer.launch({
            headless: true,
            ignoreHTTPSErrors: true
        });
        const [page] = await browser.pages();

        await page.goto(website, { waitUntil: 'networkidle2', timeout: 3000000 });

        const hrefs = await page.evaluate(() => Array.from(document.querySelectorAll('a[href]'), a => a.getAttribute('href')));

        await browser.close();
        return hrefs
    } catch (err) {
        console.error(err);
    }
}

const getSocialLinks = async (url, socialLinks) => {
    let hrefs = await getHrefsAttributes(url)
    // add array for social links which you want to fetch
    let handles = []
    hrefs.filter(href => {
        for (const link of socialLinks) {
            if (href.includes(link)) {
                handles.push({ platform: link, handle: href })
                break
            }
        }
    })

    console.log(handles);
}

getSocialLinks('https://synavos.com/', ['facebook', 'twitter', 'instagram', 'youtube', 'linkedin'])

For example this url https://netsoltech.com/, doesn't has its social media links on home page.