Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to exception handle 'list index out of range.'

I am using BeautifulSoup and parsing some HTMLs.

I'm getting a certain data from each HTML (using for loop) and adding that data to a certain list.

The problem is, some of the HTMLs have different format (and they don't have the data that I want in them).

So, I was trying to use exception handling and add value null to the list (I should do this since the sequence of data is important.)

For instance, I have a code like:

soup = BeautifulSoup(links)
dlist = soup.findAll('dd', 'title')
# I'm trying to find content between <dd class='title'> and </dd>
gotdata = dlist[1]
# and what i want is the 2nd content of those
newlist.append(gotdata)
# and I add that to a newlist

and some of the links don't have any <dd class='title'>, so what I want to do is add string null to the list instead.

The error appears:

list index out of range.

What I have done tried is to add some lines like this:

if not dlist[1]:  
   newlist.append('null')
   continue

But it doesn't work out. It still shows error:

list index out of range.

What should I do about this? Should I use exception handling? or is there any easier way?

Any suggestions? Any help would be really great!

like image 784
H.Choi Avatar asked Oct 08 '22 18:10

H.Choi


People also ask

What is list index out of range?

Generally, list index out of range means means that you are providing an index for which a list element does not exist. Now, for this exercise they require you to create a generic function for an unknown number of strings in a list(i.e. any amount of strings in a list) not just 2 strings as provided in the exercise.

How do you handle index errors?

The only way to avoid an IndexError in python is to make sure that we do not access an element from an index that is out of range. For example, if a list or a tuple has 10 elements, the elements will only be present at indices 0 to 9. So,we should never access the element at index 10 or more.

What does it mean index was out of range?

The “Index out of range” error message occurs when your graphics display settings on your computer are not set for 96 DPI.


1 Answers

Handling the exception is the way to go:

try:
    gotdata = dlist[1]
except IndexError:
    gotdata = 'null'

Of course you could also check the len() of dlist; but handling the exception is more intuitive.

like image 115
ThiefMaster Avatar answered Nov 09 '22 01:11

ThiefMaster