Python : looping through each row of html table based on headers -


i'm new python(3.4) , have parsed html table headings[ ] , rows[ ] , cells [ ] wish store each of these table (mysql) field names being items in headings[ ]

there 4 headings("data0","data1","data2","data3") there 6 rows

the code there rudimentary using beautifulsoup:

soup = beautifulsoup(r.text) table = soup.find("table") cells = [] rows = table.findall('tr') headings = [th.get_text().strip() th in table.findall("th")] row in rows:     cell in row.findall('td')     cells .append(cell.get_text().strip()) 

i'm used doing case statements, or, heaven forbid, number of if statements. place them under cell in row.findall('td') . working counter , doing like:

for row = 0 len(rows)     cell = 0 len(headings)          select case cell     case = 0        (save cell contents field called headings[0]     case = 1       (save cell contents field called headings[1]        ... 

i'm not worried saving part (yet) cant wrap head around not being able use counters. realize way beginner, should appreciate insight (and brain)


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -