@[python](IndexError: list index out of range)
python解决问题-IndexError: list index out of range
问题详解:
错误:
错误的地方:
解决方法链接:https://www.jianshu.com/p/f1b58ec12b72
正文
- 错误原因 :爬虫在做xpath时匹配到空值,正则表达式匹配的位置introduction有空值。
- 解决步骤1:对空值进行判断
if introdution:
movie={'name':name , 'director_actor':director_actor , 'info':info, 'score':score , 'evaluator':evaluator, 'introdution':introdution}
else:
movie={'name':name , 'director_actor':director_actor , 'info':info, 'score':score , 'evaluator':evaluator, 'introdution':None}
- 解决步骤2:加上try…except 错误机制跳过tr空值
try:
# name = li.xpath('//*[@id="content"]/div/div[1]/ol/li[1]/div/div[2]/div[1]/a/span[1]')
name = li.xpath(".//a/span[@class='title']/text()")[0]
director_actor=li.xpath(".//div[@class='bd']/p/text()")[0].strip()
info=li.xpath(".//div[@class='bd']/p/text()")[1].strip()
score = li.xpath(".//div[@class='star']/span[2]/text()")[0]
evaluator = li.xpath(".//div[@class='star']/span[4]/text()")[0]
introdution = li.xpath(".//p[@class='quote']/span/text()")[0]
if introdution:
movie={'name':name , 'director_actor':director_actor , 'info':info, 'score':score , 'evaluator':evaluator, 'introdution':introdution}
else:
movie={'name':name , 'director_actor':director_actor , 'info':info, 'score':score , 'evaluator':evaluator, 'introdution':None}
img = li.xpath(".//div[@class='pic']/a/img/@src")[0]
movies.append(movie)
imgUrl.append(img)
except IndexError:
pass