Last active
April 23, 2024 15:46
-
-
Save ibernabel/01ae9ec4efe5c9f83c486e88a6396929 to your computer and use it in GitHub Desktop.
Script to extract the information of the best places to work, published by Great Place To Work. To obtain the data and export it as a JSON file. URL: https://www.greatplacetowork.com/best-workplaces/100-best/2023
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let elements0 = document.querySelectorAll(".rank"); | |
let rank = Array.from(elements0).map(function(element) { | |
return element.textContent.replace(/\s+/g, ''); | |
}); | |
//--- | |
let elements1 = document.getElementsByClassName("link h5"); | |
let names = Array.from(elements1).map(function(element) { | |
return element.textContent.trim(); | |
}); | |
//--- | |
let elements2 = document.querySelectorAll(".industry > li"); | |
let industry = Array.from(elements2).map(function(element) { | |
return element.textContent; | |
}); | |
//--- | |
let elements3 = document.querySelectorAll(".location > li"); | |
let locations = Array.from(elements3).map(function(element) { | |
return element.textContent; | |
}); | |
//--- | |
let elements4 = document.querySelectorAll(".review-link > li > a"); | |
let profileLink = Array.from(elements4).map(function(element) { | |
return element.href; | |
}); | |
//--- | |
let elements5 = document.querySelectorAll(".quote"); | |
let reviews = Array.from(elements5).map(function(element) { | |
return element.textContent; | |
}); | |
// | |
let result = []; | |
// Verificar que todos los arrays tengan la misma longitud | |
if ( | |
names.length === locations.length && | |
names.length === reviews.length && | |
names.length === industry.length && | |
names.length === profileLink.length && | |
names.length === rank.length | |
) { | |
for (let i = 0; i < names.length; i++) { | |
let company = { | |
rank:rank[i], | |
name: names[i], | |
industry: industry[i], | |
location: locations[i], | |
profileLink: profileLink[i], | |
review: reviews[i], | |
rank:rank[i] | |
}; | |
result.push(company); | |
} | |
} | |
console.log(result); | |
// | |
// Convertir el objeto a JSON | |
var jsonData = JSON.stringify(result, null, 2); | |
// Crear un enlace de descarga | |
var downloadLink = document.createElement("a"); | |
downloadLink.href = "data:application/json;charset=utf-8," + encodeURIComponent(jsonData); | |
downloadLink.download = "companies-best-place-to-work-us-2023.json"; | |
downloadLink.innerHTML = "Descargar JSON"; | |
// Agregar el enlace al documento | |
document.body.appendChild(downloadLink); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We have to solve, what happens if all the arrays are not the same size. In which case the if loop will be false and the object will not be created, resulting in an empty array. Find a solution in which, if it does not find an information field, it fills it with a null or empty string.