TypeError: unsupported format string passed to NoneType.__format__

Traceback (most recent call last):
  File "C:/Users/Lenovo/PycharmProjects/untitled3/text6.py", line 49, in <module>
    printGoodsList(infoList)
序号  	价格      	商品名称            
  File "C:/Users/Lenovo/PycharmProjects/untitled3/text6.py", line 34, in printGoodsList
    print(tplt.format(count, g[0], g[1]))
   1	199.00  	<em>斯多朴<font class="skcolor_ljg">aj</font>空军一号高帮鞋篮球鞋蜘蛛侠球鞋60文小闪电哈登5联名男鞋 988-黑白红 42-标准运动码</em>
TypeError: unsupported format string passed to NoneType.__format__

分析
爬虫在提取信息时有时候会提取到空标签,而返回NoneType类。什么是NoneType类呢?简单说就是其值为None的类。而print不能打印None,所以需要过滤一遍提取的信息,把NoneType类型给去掉。
.解决办法

    for g in ilt:
        if g[1] is not None and g[0] is not None:
            count = count + 1
            print(tplt.format(count, g[0], g[1]))

猜你喜欢

转载自blog.csdn.net/qq_43274298/article/details/104638856