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

前端 未结 6 925
甜味超标
甜味超标 2020-11-27 12:53

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 cert

6条回答
  •  清歌不尽
    2020-11-27 13:46

    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.

提交回复
热议问题