首页 > 解决方案 > 如果我试图查找并发送前面有 href 标记的链接,那么 title = $(element).find('h3').text() 的等价物是什么?

问题描述

我正在尝试制作一个 Discord 机器人,它可以获取最新的帖子并在有人运行命令时将其发送出去。我已经设法通过发送帖子的标题来使其工作,但我想让它发送到帖子的链接。

帖子论坛的 HTML 看起来像

<a href="/x/y/random numbers and letters" class="post_link"></a>

有没有办法从 HTML 元素中获取这些随机数字和字母并将它们应用到机器人代码中其他地方的链接?我尝试使用 puppeteer 和cheerio 来做到这一点,但我不知道title = $(element).find('XXXXXXXXXXX').text()会是什么。

标签: node.jsweb-scrapingpuppeteercheerio

解决方案


If you're using cheerio, you'll want to do something like this:

const $ = cheerio.load('<a href="https://link-here.com/a/b/c" class="post_link"></a>');

const href = $('a').attr('href');
// "https://link-here.com/a/b/c"

推荐阅读