【问题标题】:Scraping HTML Comments using jQuery / CheerioJS?使用 jQuery / CheerioJS 抓取 HTML 注释?
【发布时间】:2021-03-13 16:35:01
【问题描述】:
我在这里有这个 HTML 代码的 sn-p:https://pastebin.com/wbQwys8R
我的目标是解析 HTML cmets,以便将它们放入字典中。这个sn-p的代码在这里
$("body").find("div.cl-entry").each((currIndex, currElement) => {
/* Get the comments from each run */
})
允许我找到所有的 HTML,这些 HTML 为我提供了上面的 HTML 代码的小 sn-p(pastebin 链接),但我如何解析 HTML cmets 本身?
【问题讨论】:
标签:
javascript
jquery
web-scraping
cheerio
【解决方案1】:
访问 div 的 .children 属性将允许您使用 Cheerio 遍历子项包括 cmets。例如:
const cheerio = require('cheerio');
const $ = cheerio.load(`
<div class='cl-info'>
<!-- updated=Saturday, 13-Mar-2021 06:46:41 GMT -->
<!-- id=985dad7f1491 -->
<!-- status=active -->
<!-- offline=no -->
<!-- name=0-30 Kiwi-SDR Flornes, Norway LB8PI -->
<!-- sdr_hw=KiwiSDR v1.438 ? DRM -->
</div>`);
for (const child of $('.cl-info')[0].children) {
if (child.type === 'comment') {
console.log(child.data);
}
}
导致以下内容被记录:
updated=Saturday, 13-Mar-2021 06:46:41 GMT
id=985dad7f1491
status=active
offline=no
name=0-30 Kiwi-SDR Flornes, Norway LB8PI
sdr_hw=KiwiSDR v1.438 � DRM