Skip to content

Instantly share code, notes, and snippets.

@ljaviertovar
Last active July 6, 2021 04:13
Show Gist options
  • Save ljaviertovar/699bff417f15be1a6be70ca37dc28d1f to your computer and use it in GitHub Desktop.
Save ljaviertovar/699bff417f15be1a6be70ca37dc28d1f to your computer and use it in GitHub Desktop.
Get the title of the first result of a book search on Amazon using web scraping.
/*
1. go to https://www.amazon.com/
2. search "programing books"
3. open the browser javascript console
*/
// Get the list of elements of the parent element. It returns a NodeList and we convert it into an array to be able to traverse it.
const elementList = Array.from(document.querySelectorAll('[data-component-type="s-search-result"]'));
// Initialize an array to store the names of the books
let namesElementList= [];
for(let element of elementList){
// Get the element where the name of the workbook is found
let divElement = element.getElementsByClassName('a-spacing-none')[1];
// Get the title of the book and save it in the array
namesElementList.push(divElement.querySelector('span').innerText)
}
console.log(namesElementList);
// Display the name of the first element
console.log(namesElementList[0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment