ssoni

scarsdale.py

Jun 24th, 2026
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. import bs4
  2. from collections import defaultdict
  3.  
  4. #counts = defaultdict(int)
  5. counts = {}
  6.  
  7. f = open("scarsdale.html", encoding="utf-8",)
  8. html = f.read()
  9.  
  10. soup = bs4.BeautifulSoup(html, features='html.parser')
  11. spans = soup.find_all('span')
  12.  
  13. for span in spans:
  14.     text = span.get_text(strip=True)
  15.     if ' - ' in text:
  16.         #the 1 is max splits, so do not split if dash is in univ. name
  17.         res = text.split(' - ',1)  
  18.         student = res[0]
  19.         college = res[1]
  20.  
  21.         #don't need to initialize if you use defaultdict.
  22.         if college not in counts:
  23.             counts[college] = 0
  24.        
  25.         counts[college] += 1
  26.  
  27. #sorted by college
  28. for college, count in sorted(counts.items()):
  29.     print(college, count)
  30.  
  31. #sorted by count
  32. for college, count in sorted(counts.items(),
  33.                              key=lambda x: x[1],
  34.                              reverse=True):
  35.     print(f"{count:3} {college}")
  36.  
Advertisement
Add Comment
Please, Sign In to add comment