Created
March 6, 2024 21:50
-
-
Save RohanAwhad/41f42cec8acb0f21016f5b7adc519a21 to your computer and use it in GitHub Desktop.
Extract details from the search response in Apollo.io
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
async function extractDataAndPaginate() { | |
const results = []; | |
function extractDataFromPage() { | |
const data = []; | |
document.querySelectorAll('tbody.zp_RFed0').forEach((tbody) => { | |
const rows = tbody.querySelectorAll('tr'); | |
rows.forEach((row) => { | |
const nameLink = row.querySelector('a[href*="/contacts/"]'); | |
const linkedInLink = row.querySelector('a[href*="linkedin.com"]'); | |
const companyLogoImg = row.querySelector('img[alt="Company logo"]'); | |
const companyNameLink = row.querySelector('a[href*="#/accounts/"]'); | |
const positionSpan = row.querySelector('td:nth-child(2) .zp_Y6y8d'); | |
const locationSpan = row.querySelector('td:nth-child(5) .zp_Y6y8d'); | |
const phoneNumberLink = row.querySelector('td:nth-child(6) a.zp-link'); | |
const skills = Array.from(row.querySelectorAll('td:nth-child(7) .zp_yc3J_')).map(el => el.textContent.trim()); | |
data.push({ | |
name: nameLink ? nameLink.textContent : null, | |
profileLink: nameLink ? nameLink.href : null, | |
linkedIn: linkedInLink ? linkedInLink.href : null, | |
companyLogo: companyLogoImg ? companyLogoImg.src : null, | |
companyName: companyNameLink ? companyNameLink.textContent : null, | |
position: positionSpan ? positionSpan.textContent : null, | |
location: locationSpan ? locationSpan.textContent : null, | |
phoneNumber: phoneNumberLink ? phoneNumberLink.textContent : null, | |
skills | |
}); | |
}); | |
}); | |
return data; | |
} | |
async function clickNextAndWaitForLoad() { | |
return new Promise((resolve, reject) => { | |
const nextPageButton = document.querySelector('button.zp-button[aria-label="right-arrow"]:not([disabled])'); | |
if (!nextPageButton) { | |
resolve('done'); | |
return; | |
} | |
const observer = new MutationObserver((mutations, obs) => { | |
obs.disconnect(); | |
resolve('loaded'); | |
}); | |
observer.observe(document, {childList: true, subtree: true}); | |
nextPageButton.click(); | |
}); | |
} | |
let status = ''; | |
do { | |
results.push(...extractDataFromPage()); | |
status = await clickNextAndWaitForLoad(); | |
} while (status !== 'done'); | |
return results; | |
} | |
function downloadResults(data, filename = 'results.json') { | |
// Convert the data to a JSON string | |
const jsonString = JSON.stringify(data); | |
// Create a blob with the JSON data | |
const blob = new Blob([jsonString], { type: 'application/json' }); | |
// Create a link element | |
const link = document.createElement('a'); | |
// Set the link's href to point to the blob | |
link.href = URL.createObjectURL(blob); | |
// Set the download attribute to the desired filename | |
link.download = filename; | |
// Append the link to the document body | |
document.body.appendChild(link); | |
// Programmatically click the link to trigger the download | |
link.click(); | |
// Remove the link from the document | |
document.body.removeChild(link); | |
} | |
/* | |
// Usage | |
extractDataAndPaginate().then((results) => { | |
console.log(results); | |
downloadResults(results) | |
}); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment