Puppeteer:select by class,但只有第一个元素
Puppeteer: select by class, but only first element
有几个 div class 包含 a=href link 的“jadejhlu”。我如何才能 select 只有第一个 div class 才能得到一个 link?我试过了
const selector = 'div.jadejhlu > a'
const links = await page.$$eval(selector, am => am.filter(e => e.href).map(e => e.href))
console.log(links)
就像这里解释的那样:
我得到了 link 的列表。我还可以尝试提取此列表的第一个 link。或者我可以尝试仅在第一个 div.
中插入 [0] 到 select
有什么想法吗?提前致谢。
您可以使用 nth-child
方法仅 select 第一个元素。
像这样:
const selector = 'div.jadejhlu > a:nth-child(1)'
改用$eval:
const selector = 'div.jadejhlu > a'
const links = await page.$eval(selector, (el) => el.href);
console.log(links)
有几个 div class 包含 a=href link 的“jadejhlu”。我如何才能 select 只有第一个 div class 才能得到一个 link?我试过了
const selector = 'div.jadejhlu > a'
const links = await page.$$eval(selector, am => am.filter(e => e.href).map(e => e.href))
console.log(links)
就像这里解释的那样:
我得到了 link 的列表。我还可以尝试提取此列表的第一个 link。或者我可以尝试仅在第一个 div.
中插入 [0] 到 select有什么想法吗?提前致谢。
您可以使用 nth-child
方法仅 select 第一个元素。
像这样:
const selector = 'div.jadejhlu > a:nth-child(1)'
改用$eval:
const selector = 'div.jadejhlu > a'
const links = await page.$eval(selector, (el) => el.href);
console.log(links)