Created
August 20, 2021 07:20
-
-
Save ryan-mooore/ba19fec8fd0dc13b4543aabd17bd63c8 to your computer and use it in GitHub Desktop.
Dictionary comprehension to convert an HTML <table> element to a python dictionary using BeautifulSoup
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
from bs4 import BeautifulSoup | |
soup = BeautifulSoup("https://github.com") | |
table = soup.table | |
result_dict = { | |
header.text: { | |
row.find_all("td")[0].text: row.find_all("td")[index].text | |
for row in table.tbody.find_all("tr") | |
} | |
for index, header in enumerate(table.find_all("th")[1:], start=1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment