-2

I am using python 3.6, and i'm trying to scrap a table from an website, while scrapping i got all the datas from table but at the end of the result i got the below error message.

IndexError Traceback (most recent call last)
<ipython-input-29-45703b14c6ee> in <module>()
      1 for tr in soup.find_all('tr')[2:]:
      2     tds = tr.find_all('td')
----> 3     print(tds[0].text, tds[1].text, tds[2].text)

IndexError: list index out of range

what is wrong in this code? what is the solution?please help

KUSHA B K
  • 1,489
  • 13
  • 20
  • 1
    `tds` does not have 3 elements. You try access a index there does not exists. Try to debug by printing out the full array of tds before line 3. – jhilmer Nov 21 '17 at 10:53

1 Answers1

1

You might want to check the length of tds before trying to access the elements. You cannot access elements that do not exist.

for tr in soup.find_all('tr')[:]:
    tds = tr.find_all('td')
    tds_length = len(tds)
    # using tds_length, get what you want
    # if tds_length < 2: print(tds[0].text)

Or

for tr in soup.find_all('tr')[:]:
    tds = tr.find_all('td')
    try:
       print(tds[0].text)
    except IndexError:
       pass
    else: 
       try:
           print(tds[1].text)
       except IndexError:
           pass
       else:
           try:
               print(tds[2].text)
           except IndexError:
               pass
omushpapa
  • 1,663
  • 20
  • 25
  • second solution is good no error – KUSHA B K Nov 21 '17 at 11:00
  • 1
    Why complicate this with nested statements? If you want to print all results, `for td in tds: print(td.text)`. If you want to print up to 3, `length = len(tds) if len(tds) < 4 else 3; for i in range(length): print(tds[i].text)`. – Reti43 Nov 21 '17 at 11:03