Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bs4
- from collections import defaultdict
- #counts = defaultdict(int)
- counts = {}
- f = open("scarsdale.html", encoding="utf-8",)
- html = f.read()
- soup = bs4.BeautifulSoup(html, features='html.parser')
- spans = soup.find_all('span')
- for span in spans:
- text = span.get_text(strip=True)
- if ' - ' in text:
- #the 1 is max splits, so do not split if dash is in univ. name
- res = text.split(' - ',1)
- student = res[0]
- college = res[1]
- #don't need to initialize if you use defaultdict.
- if college not in counts:
- counts[college] = 0
- counts[college] += 1
- #sorted by college
- for college, count in sorted(counts.items()):
- print(college, count)
- #sorted by count
- for college, count in sorted(counts.items(),
- key=lambda x: x[1],
- reverse=True):
- print(f"{count:3} {college}")
Advertisement
Add Comment
Please, Sign In to add comment