IndexError: list index out of range错误一种特殊原因

原来代码如下

plt = re.findall(r’“view_price”:"[\d|.]"’, html)
tlt = re.findall(r’“raw_title”:".
?"’, html)
item = re.findall(r’“item_loc”:".*?"’, html)
for i in range(len(plt)):
price = plt[i].split(’:’)[1]
title = tlt[i].split(’:’)[1]
item = item[j].split(’:’)[1]


这是运行后错误
我仔细查看数组发现并没有越界
然后用try: except:来获取这个错误发现并没有用
后面快绝望的时候发现正则表达式获取内容的变量和数组变量item相同
感觉有点不对劲
后面重新命名了下面的item变为item1
代码如下

plt = re.findall(r’“view_price”:"[\d|.]"’, html)
tlt = re.findall(r’“raw_title”:".
?"’, html)
item = re.findall(r’“item_loc”:".*?"’, html)
for i in range(len(plt)):
price = plt[i].split(’:’)[1]
title = tlt[i].split(’:’)[1]
item1 = item[i].split(’:’)[1]
ulist.append([price, title,item1])

成功解决问题
所以小伙伴们写代码时要细心哦

posted @ 2019-07-20 11:50  X_J  阅读(501)  评论(0编辑  收藏  举报