【发布时间】:2023-03-07 00:30:01
【问题描述】:
我的 Cheerio 代码如下所示:
const title = $("meta")
.filter(function () {
return (
($(this).attr("property") != null &&
$(this).attr("property").endsWith("title")) ||
);
}).attr("content")
我想将此迁移到使用客户端 javascript 的 puppeteer。到目前为止我有这个:
const title = Array.from(document.querySelectorAll("meta")
.filter(function () {
return {
// stuck here: how do I call this?
// $(this)
我被困在如何使用文档查询选择器语法来引用“this”。
【问题讨论】:
-
请注意,问题中的代码可以完全重写为
const elem = document.querySelector("meta[property$='title']); const title = elem && elem.getAttribute("content");同样,您的答案中的代码只是document.queySelectorAll("meta[name$='title']")
标签: javascript jquery web-scraping puppeteer cheerio