-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape_discord_clubs.py
More file actions
125 lines (108 loc) · 4.4 KB
/
Copy pathscrape_discord_clubs.py
File metadata and controls
125 lines (108 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from selenium.webdriver.common.by import By
import time, csv
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
# Setup
chromedriver_path = input("🔧 Enter the full path to your chromedriver executable: ").strip()
options = Options()
options.add_argument("--start-maximized")
service = Service(chromedriver_path)
driver = webdriver.Chrome(service=service, options=options)
driver.get("https://discord.com/login")
input("🔓 Log into Discord manually if needed...\n⚠️ Make sure you're on the Home tab and it’s fully visible, then press Enter...")
# =====================
# SCROLL FUNCTION
# =====================
scroll_container = driver.find_element(By.XPATH, "//div[contains(@class,'scrollerBase_')]")
for _ in range(40):
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", scroll_container)
time.sleep(0.3)
# =====================
# SCRAPE FUNCTION
# =====================
def scrape_cards(category_label):
scroll_container = driver.find_element(By.XPATH, "//div[contains(@class,'scrollerBase_')]")
for _ in range(40):
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", scroll_container)
time.sleep(0.3)
results = []
cards = driver.find_elements(By.XPATH, "//div[contains(@class, 'card_')]")
for card in cards:
# skip the "Add Server" card
if "Add Server" in card.text:
continue
try:
name = card.find_element(By.XPATH, ".//div[contains(@class, 'guildName')]").text.strip()
except:
name = ""
try:
desc = card.find_element(By.XPATH, ".//div[contains(@class, 'description')]").text.strip()
except:
desc = ""
try:
member_divs = card.find_elements(By.XPATH, ".//div[contains(@class, 'memberCount')]")
members = ""
for div in member_divs:
text = div.text.strip()
if "Member" in text and "Online" not in text:
members = text.split()[0]
break
except:
members = ""
results.append([name, desc, members, category_label, "", "", "", "", "", "", ""])
return results
# =====================
# STEP 1: SCRAPE HOME
# =====================
print("🏠 Scraping: Home tab...")
home_clubs = scrape_cards("Home")
home_names = set(row[0].strip().lower() for row in home_clubs if row[0].strip())
# =====================
# STEP 2: SCRAPE CATEGORIES
# =====================
categories = {
"Clubs": "Clubs",
"Classes & Subjects": "Classes & Subjects",
"Social & Study": "Social & Study",
"Miscellaneous": "Miscellaneous"
}
categorized_clubs = []
for tab_text, category_label in categories.items():
print(f"📂 Scraping category: {category_label}...")
try:
tab = driver.find_element(By.XPATH, f"//div[contains(text(), '{tab_text}')]")
tab.click()
time.sleep(2)
categorized_clubs += scrape_cards(category_label)
except Exception as e:
print(f"❌ Couldn’t click tab '{tab_text}': {e}")
continue
driver.quit()
# =====================
# STEP 3: FIND UNCATEGORIZED
# =====================
categorized_names = set(c[0].strip().lower() for c in categorized_clubs if c[0].strip())
home_names_clean = set(h[0].strip().lower() for h in home_clubs if h[0].strip())
uncategorized_names = home_names_clean - categorized_names
uncategorized_clubs = []
for row in home_clubs:
normalized_name = row[0].strip().lower()
if normalized_name in uncategorized_names:
updated_row = row[:]
updated_row[3] = "Uncategorized"
uncategorized_clubs.append(updated_row)
# =====================
# STEP 4: COMBINE + SAVE
# =====================
final_data = categorized_clubs + uncategorized_clubs
with open("discord_clubs.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow([
"Club Name", "Club Description", "No. of members", "Club Category",
"Message Sent?", "Admins Replied?", "Admins Response", "Meeting planned",
"Admins Discord Handle", "Admin Type (Admin/President/Moderator)", "Notes"
])
writer.writerows(final_data)
print(f"✅ Final CSV saved as 'discord_clubs.csv' with {len(final_data)} entries.")
print(f"🧩 {len(uncategorized_clubs)} clubs were only in Home tab and marked as 'Uncategorized'.")