鱼C论坛

 找回密码
 立即注册
查看: 1507|回复: 2

[已解决]错误不知道怎么产生的

[复制链接]
发表于 2019-8-16 10:47:42 | 显示全部楼层 |阅读模式

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

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

x
<ol class="tuiMusic">
<li>
  <span class="songNum">
   1.
  </span>
  <a class="play" href="/play/464446.htm" target="_self">
  </a>
  <a class="songNameA" href="/play/464446.htm" target="_self">
   等你等了那么久
  </a>
  <a href="/geshou/7283.htm" target="_blank">
   <span class="jianNum">
    祁隆
   </span>
  </a>
</li>
<li>
  <span class="songNum">
   2.
  </span>
  <a class="play" href="/play/637791.htm" target="_self">
  </a>
  <a class="songNameA" href="/play/637791.htm" target="_self">
   小苹果
  </a>
  <a href="/geshou/18348.htm" target="_blank">
   <span class="jianNum">
    筷子兄弟
   </span>
  </a>
</li>

def get_data(res):
    soup = bs4.BeautifulSoup(res.text,"html.parser")
    data = []
    targets = soup.find("ol",class_ = "tuiMusic")
    targets = targets.find_all("li")
   
    print(targets.prettify())
   
"ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'prettify'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?

就是上面的简单数据,我想遍历所有的li为什么就会出错呢。
遇见好几次这种错误了。麻烦大佬帮忙指点。错误原因是什么,应该怎么解决呢。


最佳答案
2019-8-16 13:53:07
因为你是用一个ResultSet集合类型去调用的prettify()函数,所以报错了,要想正常调用,你需要对这个列表遍历,用里面的每一个元素去调用。

修改代码如下:

  1. def get_data(res):
  2.     soup = bs4.BeautifulSoup(res.text, "html.parser")
  3.     data = []
  4.     targets = soup.find("ol", class_="tuiMusic")
  5.     targets = targets.find_all("li")

  6.     for target in targets:
  7.         print(target.prettify())
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-16 13:53:07 | 显示全部楼层    本楼为最佳答案   
因为你是用一个ResultSet集合类型去调用的prettify()函数,所以报错了,要想正常调用,你需要对这个列表遍历,用里面的每一个元素去调用。

修改代码如下:

  1. def get_data(res):
  2.     soup = bs4.BeautifulSoup(res.text, "html.parser")
  3.     data = []
  4.     targets = soup.find("ol", class_="tuiMusic")
  5.     targets = targets.find_all("li")

  6.     for target in targets:
  7.         print(target.prettify())
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2019-8-16 14:26:58 | 显示全部楼层
空青 发表于 2019-8-16 13:53
因为你是用一个ResultSet集合类型去调用的prettify()函数,所以报错了,要想正常调用,你需要对这个列表遍 ...

非常感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-4 08:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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