node.js - 如果我试图查找并发送前面有 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()
会是什么。
解决方案
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"
推荐阅读
- java - 调用多线程 C++ 代码时 JNI 是否正常工作?
- sql - 如何在 django 中使用 NOT IN sql 查询
- google-maps - 启用地理编码 API 时,Google Maps JavaScript API 抛出“ExpiredKeyMapError”
- javascript - 如何修复这个简单的均值计算器以使其正常工作,以及如何制作它以便它需要尽可能多的数字?
- python - xlsxwriter pandas frame:如果列中有空白单元格,则突出显示行
- electron - Electron:如何删除不需要的模块?
- javascript - 谁将 mp4 文件转换为 Wav 音频文件?
- c# - 如何将 ping 的用户名和变量传递给机器人命令
- generics - 特质不能变成对象
- node.js - 如何将模型结果传递给节点 js 中的视图