Wrote some code for extracting the top 5 fraternities and sororities from the Greek Rank site. For some reason the output does not contain any of the desired elements extracted from the site.
import requests
from bs4 import BeautifulSoup
URL = "https://www.greekrank.com/uni/51/greek-life/"
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
cards = soup.find_all('div', class_='card-body')
print("Top 5 Fraternities/Sororities:")
print("-----------------------------------")
print("| Ranking | Fraternity/Sorority |")
print("-----------------------------------")
for i, card in enumerate(cards):
if i == 5:
break
ranking = card.find('h5', class_='card-title').text.strip()
name = card.find('h6', class_='card-subtitle mb-2 text-muted').text.strip()
print(f"| {ranking:<8} | {name:<23} |")
print("-----------------------------------")
CodePudding user response:
Class names have changed I guess. Here's my attempt at it:
import requests
from bs4 import BeautifulSoup
URL = "https://www.greekrank.com/uni/51/greek-life/"
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
all_frats = soup.find('div', class_='fraternity-rankings')
frats = all_frats.findChildren('div',class_='row')
print("Top 5 Fraternities/Sororities:")
print("-----------------------------------")
print("| Ranking | Fraternity/Sorority |")
print("-----------------------------------")
for i, frat in enumerate(frats):
if i == 5:
break
cells=frat.findChildren('div',class_='cell')
ranking = cells[4].text.strip().split()[1]
name = cells[2].find('a').text.strip()
print(f"| {ranking:<8} | {name:<23} |")
print("-----------------------------------")