I don\'t know if there is such a thing - but I\'m trying to do an ordered dict comprehension. However it doesn\'t seem to work?
import requests
from bs4 imp
You can't directly do a comprehension with an OrderedDict
. You can, however, use a generator in the constructor for OrderedDict
.
Try this on for size:
import requests
from bs4 import BeautifulSoup
from collections import OrderedDict
soup = BeautifulSoup(html, 'html.parser')
tables = soup.find_all('table')
rows = tables[1].find_all('tr')
t_data = OrderedDict((row.th.text, row.td.text) for row in rows if row.td)