Skip to content

Instantly share code, notes, and snippets.

@ryan-mooore
Created August 20, 2021 07:20
Show Gist options
  • Save ryan-mooore/ba19fec8fd0dc13b4543aabd17bd63c8 to your computer and use it in GitHub Desktop.
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
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