Based on the selector I assume you are in this step interested in the navbar items.
const results = await page.$$eval(".navbar-nav", navBars => {
return navBars.map(navBar => {
const anchors = Array.from(navBar.getElementsByTagName('a'));
return anchors.map(anchor => anchor.innerText);
});
})
This yields [ [ 'Coronavirus', 'Population' ] ]
and might be useful for you.
Use $eval
if you expect only one element and $$eval
if you expect multiple elements. In the callback you have a reference to that dom element, but you cannot return it directly. If you console.log
anything it won't show up in the nodejs terminal, but in the browsers terminal. What you return there will be send back to nodejs and it needs to be serializable (I think). What you get back from navBar
will be converted to an empty object and is not what you want. That's why I map over it and convert it to a string (innerText
).
If you want scrape other data, you should use another selector (.nav-bar
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…