TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

问题描述:

自学初期这个问题怎么处理。
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

img

你 a字典中没有href键名。a.get('href') 获取的是 None

None 不能用 + 号与"\n"字符串拼接

可以改成
href = a.get('href',"")
当a字典中没有href键名时会返回""空字符串

如有帮助,望采纳!谢谢!

报错信息说的很明确哈,herf是None空的

if herf != None:
    test+=herf+'\n'


 觉得有用的话采纳一下哈