Skip to content

Instantly share code, notes, and snippets.

@BytefishMedium
Last active November 19, 2021 06:31
Show Gist options
  • Save BytefishMedium/43100f3052dfdc41450224ee0eabcccf to your computer and use it in GitHub Desktop.
Save BytefishMedium/43100f3052dfdc41450224ee0eabcccf to your computer and use it in GitHub Desktop.
var followers = [];
var totalFollowers = getTotalFollowers()
console.log(`You have ${totalFollowers} follower, crawler start now.`)
var timer = setInterval(() => {
let liElements = document.querySelectorAll("ul > li");;
let isEnding = followers.length + liElements.length == totalFollowers;
if (isEnding) {
for (let index = 0; index < liElements.length; index++) {
let follower = parseDataFromLiElement(liElements[index]);
followers.push(follower);
}
console.log("-- crawling over --");
let result = count(followers)
console.log(count(followers))
alert(count(followers))
clearInterval(timer);
return;
}
// scroll down the webpage
window.scrollTo(0, document.body.scrollHeight);
if (liElements.length > 80) {
// parse first 50
for (let index = 0; index < 50; index++) {
let follower = parseDataFromLiElement(liElements[index]);
// console.log('follower: ', follower)
followers.push(follower);
}
console.log(`The crawler have crawled ${followers.length} followers.`)
console.log(count(followers))
// delete some element to reduce memory usage
for (let index = 0; index < 50; index++) {
liElements[index].remove();
}
}
}, 100);
// parse data from li elements
function parseDataFromLiElement(li) {
return {
name: li.querySelector("h2").textContent,
url: li.querySelector("a").href,
isMembership: li.querySelector("svg") != null,
description: li.querySelector("p")?.textContent,
};
}
function getTotalFollowers() {
let totalFollowersElement = document.querySelector("h2");
let numberString = totalFollowersElement.innerText.split(" ")[0];
return parseInt(numberString.split(",").join(""));
}
function count(followers){
let membersCount = followers.filter((item) => item.isMembership).length;
let percentage = (membersCount / followers.length).toFixed(4)
return `total followers: ${followers.length}, total membership: ${membersCount}, percent: ${percentage * 100}%`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment