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

前端 未结 6 918
甜味超标
甜味超标 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条回答
  •  Happy的楠姐
    2020-11-27 13:56

    You have two options; either handle the exception or test the length:

    if len(dlist) > 1:
        newlist.append(dlist[1])
        continue
    

    or

    try:
        newlist.append(dlist[1])
    except IndexError:
        pass
    continue
    

    Use the first if there often is no second item, the second if there sometimes is no second item.

提交回复
热议问题