Skip to content

Instantly share code, notes, and snippets.

@roshanmirajkar
Created August 24, 2024 20:05
Show Gist options
  • Save roshanmirajkar/61096ad944beee40104e2e43508c5863 to your computer and use it in GitHub Desktop.
Save roshanmirajkar/61096ad944beee40104e2e43508c5863 to your computer and use it in GitHub Desktop.
TikTok Follower Scraper - Scrapes usernames from TikTok follower lists and exports to CSV using Chrome Console
/**
* Scrapes TikTok follower usernames by scrolling through the user's followers list.
* The script will scroll to the bottom of the page multiple times and extract
* follower usernames from the currently loaded list. It then exports the collected usernames
* as a CSV file.
*
* To use:
* 1. Open the browser's Developer Tools (usually with F12 or right-click → Inspect).
* 2. Go to the "Console" tab.
* 3. Paste this script and press "Enter".
* 4. A CSV file will automatically be downloaded after the script finishes.
*/
async function scrapeFollowers() {
const followers = new Set(); // Use a Set to ensure unique followers
/**
* Function to scroll to the bottom of the page.
* Returns a promise that resolves after a 2-second delay to allow dynamic content to load.
*/
const scrollToBottom = async () => {
window.scrollTo(0, document.body.scrollHeight);
return new Promise(resolve => {
setTimeout(resolve, 2000); // Wait 2 seconds for content to load
});
};
// Initialize variables to track page height and scroll attempts
let previousHeight = document.body.scrollHeight;
let scrollAttempts = 0;
/**
* Scroll and extract usernames.
* This loop will scroll the page until no new content is loaded for a maximum of 20 attempts.
*/
while (scrollAttempts < 20) { // Allow a maximum of 20 scroll attempts
await scrollToBottom(); // Scroll to the bottom
// Extract usernames from the loaded follower list
document.querySelectorAll('p.css-3gbgjv-PUniqueId.es616eb8')
.forEach(user => followers.add(user.innerText.trim())); // Add usernames to the Set
// Check if more content is loaded, otherwise increment scrollAttempts
const newHeight = document.body.scrollHeight;
if (newHeight === previousHeight) {
scrollAttempts++; // Increase scroll attempts if no new content is loaded
} else {
scrollAttempts = 0; // Reset attempts if new content is loaded
}
previousHeight = newHeight;
}
// Log the number of unique followers collected
console.log(`Number of unique followers: ${followers.size}`);
// Convert the Set to an Array for easier manipulation and CSV export
const followersArray = Array.from(followers);
// Log the followers array for debugging/review purposes
console.log(followersArray);
// Prepare and download the collected data as a CSV file
const csvContent = "data:text/csv;charset=utf-8," + followersArray.join("\n");
const encodedUri = encodeURI(csvContent);
const link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "tiktok_followers.csv");
document.body.appendChild(link);
link.click(); // Automatically trigger the download
}
// Execute the scraping function
scrapeFollowers();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment