有幾個 div 類“jadejhlu”包含 aa=href 鏈接。如何僅選擇第一個 div 類以僅獲取一個鏈接?我試過
const selector = 'div.jadejhlu > a'
const links = await page.$$eval(selector, am => am.filter(e => e.href).map(e => e.href))
console.log(links)
就像這里解釋的那樣:Puppeteer - Retrieving links from divs with specific class names
我得到一個鏈接串列。我也可以嘗試提取此串列的第一個鏈接。或者我可以嘗試插入 [0] 以僅選擇第一個 div。
有任何想法嗎?提前致謝。
uj5u.com熱心網友回復:
您可以使用nth-child方法僅選擇第一個元素。
像這樣的東西:
const selector = 'div.jadejhlu > a:nth-child(1)'
uj5u.com熱心網友回復:
改用$eval:
const selector = 'div.jadejhlu > a'
const links = await page.$eval(selector, (el) => el.href);
console.log(links)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416560.html
標籤:
