Created
April 26, 2021 20:05
-
-
Save jiobu1/dc8eb4dca368b0c66f3764216e0198a4 to your computer and use it in GitHub Desktop.
beautfiul soup -> parsing table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
table = soup.find("table", { "class" : "" }) | |
for row in table.find_all("tr"): | |
cell = row.find_all("td") | |
if len(cell) == 7: | |
school = row.find('a', {'class':'name'}).text.strip() | |
try: | |
score = row.find('div', {'class': 'circle-rating--small'}).text.strip() | |
except AttributeError: | |
score = '0/10' | |
rating = row.find('div', {'class': 'scale'}).text.strip() | |
try: | |
address = row.find('div', {'class': 'address'}).text.strip() | |
except AttributeError: | |
address = "Unavailable" | |
school_type = cell[1].find(text=True) | |
grade = cell[2].find(text=True) | |
students = cell[3].find(text=True) | |
student_teacher_ratio = cell[4].find(text=True) | |
try: | |
district = cell[6].find(text=True) | |
except AttributeError: | |
district = 'Unavailable' | |
records.append({ | |
'School': school, | |
'Score': score, | |
'Rating': rating, | |
'Address': address, | |
'Type': school_type, | |
'Grades' : grade, | |
'Total Students Enrolled': students, | |
'Students per teacher' : student_teacher_ratio, | |
'District': district | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment