I am parsing data from a website but getting the error "IndexError: list index out of range". But, at the time of debugging I got all the values. Previously, it worked completely fine, but suddenly can't understand why I am getting this error.
str2 = cols[1].text.strip()
IndexError: list index out of range
Here is my code.
import requests
import DivisionModel
from bs4 import BeautifulSoup
from time import sleep
class DivisionParser:
def __init__(self, zoneName, zoneUrl):
self.zoneName = zoneName
self.zoneUrl = zoneUrl
def getDivision(self):
response = requests.get(self.zoneUrl)
soup = BeautifulSoup(response.content, 'html5lib')
table = soup.findAll('table', id='mytable')
rows = table[0].findAll('tr')
division = []
for row in rows:
if row.text.find('T No.') == -1:
cols = row.findAll('td')
str1 = cols[0].text.strip()
str2 = cols[1].text.strip()
str3 = cols[2].text.strip()
strurl = cols[2].findAll('a')[0].get('href')
str4 = cols[3].text.strip()
str5 = cols[4].text.strip()
str6 = cols[5].text.strip()
str7 = cols[6].text.strip()
divisionModel = DivisionModel.DivisionModel(self.zoneName, str2, str3, strurl, str4, str5, str6, str7)
division.append(divisionModel)
return division
These are the values at the time of debugging:
str1 = {str} '1'
str2 = {str} 'BHUSAWAL DIVN-ENGINEERING'
str3 = {str} 'DRMWBSL692019t1'
str4 = {str} 'Bhusawal Division - TRR/P- 44.898Tkms & 2.225Tkms on 9 Bridges total 47.123Tkms on ADEN MMR &'
str5 = {str} 'Open'
str6 = {str} '23/12/2019 15:00'
str7 = {str} '5'
strurl = {str} '/works/pdfdocs/122019/51822293/viewNitPdf_3021149.pdf'
len(cols) < 2. We don't have the input to your program, which can explain why this is the case, so just look into it yourself and decide what to do with it (e.g., remove those specific rows, fix them, etc).IndexErroron the linecols[1].text.strip()implies thatlen(cols) < 2???