I’m trying to parse a table with BeautifulSoup and finding that it is/would be helpful to know what row and column I’m looking at as I walk through it. Right now I’ve got this:
for table in soup.find_all("table", {"class":"foo"}):
r = 0
for row in table.find_all('tr'):
cells = row.find_all("td")
c = 0
for cell in cells:
print "row", r, "cell", c
print cell.attr
c += 1
r +=1
This dumps out some revealing info:
row 0 cell 0
row 1 cell 0
row 1 cell 1
row 1 cell 2
row 1 cell 3
row 1 cell 4
row 2 cell 0
row 2 cell 1
row 3 cell 0
row 3 cell 1
For some reason row[1] has a lot of extra columns. Handy to know. What I’m wondering is … is there a built-in variable that would report my place in the list.
Are you looking for
enumerate?