这段编程总提示list index out of range,看半天想不通~求解代码如下
def saveData(datalist,savepath):
book=xlwt.Workbook(encoding='utf-8',style_compression=0) sheet=book.add_sheet('豆瓣最受欢迎影评',cell_overwrite_ok=True) col=('标题','作者','影片名','影片详情链接','推荐级','回应数','影评链接','影评','有用数') for i in range(0,9): sheet.write(0,i,col[i])#列名 for i in range(0,50):#总共50条影评 data=datalist[i] for j in range(0,9): sheet.write(i+1,j,data[j])#数据 book.save(savepath)#保存
def main():
baseurl='http://movie.douban.com/review/best/?start=' datalist=getData(baseurl) savapath=u'豆瓣最受欢迎影评.xlsx' saveData(datalist,savapath)
main()
The list is out of bounds. Are you sure the length of each datalist is 9? Is it possible that when crawling a web page, some data is missing and not assigned a value? As a result, each datalist may have different lengths. Cause
for j in range(0,9)
When data[j] goes out of bounds?I agree with the above. The information given by the poster is incomplete and there is no contextual information about the error. You can only print it out and compare it yourself.
The list is out of bounds, why didn’t I post the error message