下面是2个产品的价格
final_price = li.xpath('.//span[@class="price"]/text()')[0]
pre_price = li.xpath('.//span[@class="price-rrp__price"]/text()')[0]
if len(pre_price)==0:
print("list is empty")
else:
pre_price = li.xpath('.//span[@class="price-rrp__price"]/text()')[0]
print(final_price, pre_price)
17,98 € 29,99 €
完成
44,00 € 54,99 €
完成
160,08 € 194,99 €
完成
Traceback (most recent call last):
File "C:\Users\86134\PycharmProjects\pythonProject\use_kuaidaili.py", line 56, in
pre_price = li.xpath('.//span[@class="price-rrp__price"]/text()')[0]
IndexError: list index out of range
返回有些结果是空值,但是出现报错。是不是用try else?谢谢
直接检查返回结果不用去列表某个值。后面结果分别才进行赋值。