Created
August 1, 2024 20:53
-
-
Save kellenmace/a3b564e4de72e44bd20f969c88a11a8d to your computer and use it in GitHub Desktop.
HTML scraping code
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
/** | |
* HTML scraping code | |
* | |
* Steps to use: | |
* 2. Open the browser console, paste in the entire contents of this file and hit <Enter>. | |
* 3. Copy the console output and paste it into a text editor. Save it with a .csv extension. | |
*/ | |
(function() { | |
const removeCommas = (string) => string.replace(/,/g, ``); | |
function getAnalyticsPageData() { | |
const lines = [...document.querySelectorAll("#content-data-box .data.fade-in")]; | |
return lines.map(extractDataFromLine); | |
} | |
function extractDataFromLine(line) { | |
const path = removeCommas(line.childNodes[0].innerText); | |
const url = removeCommas(line.childNodes[0].childNodes[0].href); | |
const entries = removeCommas(line.childNodes[2].childNodes[1].title); | |
const visitors = removeCommas(line.childNodes[3].childNodes[1].title); | |
const views = removeCommas(line.childNodes[4].childNodes[1].title); | |
const bounces = removeCommas(line.childNodes[5].childNodes[1].title); | |
const avgTime = removeCommas(line.childNodes[6].childNodes[1].title); | |
return { path, url, entries, visitors, views, bounces, avgTime }; | |
} | |
function generateCsvOutput(csvData) { | |
const headers = ["Path", "URL", "Entries", "Visitors", "Views", "Bounces", "Avg. Time"]; | |
const lastIndex = csvData.length - 1; | |
const isLastItem = (index) => index === lastIndex; | |
let csvOutput = headers.join(`,`) + `\n`; | |
csvData.forEach((row, index) => { | |
csvOutput += Object.values(row).join(`,`); | |
if (!isLastItem(index)) { | |
csvOutput += `\n`; | |
} | |
}); | |
return csvOutput; | |
} | |
function init() { | |
const analyticsPageData = getAnalyticsPageData(); | |
const csvOutput = generateCsvOutput(analyticsPageData); | |
copy(csvOutput); // Copy the CSV to your clipboard | |
console.log(csvOutput); | |
console.log( | |
`☝🏼CSV data is above. Copy & paste it into a text editor and save it with a .csv extension.` | |
); | |
} | |
init(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment