鱼C论坛

 找回密码
 立即注册
查看: 2090|回复: 3

[已解决]求助

[复制链接]
发表于 2023-4-28 17:14:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
from bs4 import BeautifulSoup
tree_DOM = BeautifulSoup(str_html)
div =  tree_DOM.find('div', id="J_goodsList")
lst_li = div.find_all('li')
lst_li
for li in lst_li:
    price = li.find('div',class_="p-price").find('i').string
    name = li.find("div",class_="p-name p-name-type-2").find("i").string
    printer = li.find("div",class_="curr-shop hd-shopname").find("i").strings()

    #bk_id = li.find(xxxxxxx)
                    
    print(price)
    print(name)
    print(printer)                  
                     
   
    print('-----------')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [25], in <cell line: 6>()
      7 price = li.find('div',class_="p-price").find('i').string
      8 name = li.find("div",class_="p-name p-name-type-2").find("i").string
----> 9 printer = li.find("div",class_="curr-shop hd-shopname").find("i").strings()
     11 #bk_id = li.find(xxxxxxx)
     13 print(price)

AttributeError: 'NoneType' object has no attribute 'find'
在报错请问如何修改 谢谢
最佳答案
2023-4-29 11:15:13
这个报错意味着在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())
这样就可以将迭代器中的所有字符串拼接成一个字符串。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-28 17:21:26 | 显示全部楼层
没有找到这个元素  li.find("div",class_="curr-shop hd-shopname")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-28 17:35:49 | 显示全部楼层
找不到这个元素,因为requests或得到的是动态渲染钱的东西,selenium可以获得动态渲染之后的东西
比如你在scirpt里把标签改了,开发者模式得到的是改之后的东西,reqeusts是改之前的东西,两者各有利弊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-29 11:15:13 | 显示全部楼层    本楼为最佳答案   
这个报错意味着在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())
这样就可以将迭代器中的所有字符串拼接成一个字符串。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-23 11:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表