Skip to content

Instantly share code, notes, and snippets.

@gavin19
Last active March 8, 2020 11:25
Show Gist options
  • Save gavin19/13cf4727af4a2362e95c2745bb11e5dd to your computer and use it in GitHub Desktop.
Save gavin19/13cf4727af4a2362e95c2745bb11e5dd to your computer and use it in GitHub Desktop.
Scrape avatars from the subreddit mods page
import requests
from bs4 import BeautifulSoup as bs
from time import sleep
HEADERS = {"user-agent": "win64:avatar-scraper:/u/gavin19"}
USER_CLS = "_2Q3rLIRb_ij54AEsabVm9L"
PAGE_CLS = "_2QinjfjfDTG6Df4_Tb_eRN"
MOD_PAGE = "https://new.reddit.com/r/pics/about/moderators/"
tmp_page = MOD_PAGE
avis = {}
while True:
req = requests.get(tmp_page, headers=HEADERS)
h = bs(req.text, "html.parser")
mods = h.find_all(class_=USER_CLS)
for m in mods:
u = m.attrs['href'].split("/")[-1]
i = m.find("img")
i = i.attrs['src'].split("?")[0]
avis[u] = i
n = h.find(class_=PAGE_CLS)
u = n.find("a").attrs['href']
if "after" in u:
tmp_page = MOD_PAGE + u.split("/")[-1]
sleep(1)
else:
break
print(avis)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment