Hello I am trying to scrape a web page and return all of the links inside example of the html element:
<a href="#/item/2sDSXbG">
<a href="#/item/4ssaSXbG">
<a href="#/item/Sawd432">
Here is my code:
let links = [];
let elements2 = document.querySelectorAll('a');
for (var element2 of elements2)
links.push(element2.textContent);
After I return the value and print it I get an Error telling me that my variable is not defined My Error:
UnhandledPromiseRejectionWarning: ReferenceError: links is not defined
End Goal: My goal is to be able to be able to create an array of all the items in the list. I would than later parse the information so that it is just the text after /item/
With $$eval:
let hrefs = await page.$$eval('a', as => as.map(a => a.href))
It seems this is what you need to achieve your goal with puppeteer:
const hrefs = await page.evaluate(() => {
let links = [];
let elements2 = document.querySelectorAll('a');
for (let element2 of elements2)
links.push(element2.href);
return links;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With