这个报错意味着在li元素中没有找到class="curr-shop hd-shopname"的div元素,因此返回了None,导致后续调用find("i")方法时出现了'NoneType' object has no attribute 'find'错误。
要解决这个问题,你可以在访问find("i")方法之前,先判断是否成功找到了div元素,例如:
- printer = li.find("div", class_="curr-shop hd-shopname")
- if printer is not None:
- printer = printer.find("i").string
- else:
- printer = None
复制代码
这样,在找到了div元素后,再调用find("i")方法来获取内部的字符串即可。如果没有找到,则将printer赋值为None,以避免后续的错误。
另外,strings()方法返回的是一个迭代器对象,而不是字符串。如果你想获取其中的字符串,可以使用join()方法,例如:
- printer = "".join(li.find("div", class_="curr-shop hd-shopname").find("i").strings())
复制代码
这样就可以将迭代器中的所有字符串拼接成一个字符串。