By looking at your code, an assumption is made in the loop that there will always be (at least) 2 td
elements in the list of found tr
elements. If there are some case where a tr
element contains less than 2 elements, an IndexError will be raised.
Try changing the loop to something like this:
for tr in soup.find_all('tr')[2:]:
tds = tr.find_all('td')
if len(tds) >= 2:
print tds[0].text, tds[1].text
The check where the number of td
elements must be 2 or more is specific for the page you are parsing and I guess that you want the two values written together. A more general solution could be:
for tr in soup.find_all('tr')[2:]:
for td in tr.find_all('td'):
print td.text