鱼C论坛

 找回密码
 立即注册
查看: 4226|回复: 5

[已解决]照着小甲鱼写的不明白为什么会报错

[复制链接]
发表于 2021-5-22 21:59:35 | 显示全部楼层 |阅读模式
8鱼币


  1. import requests
  2. import bs4
  3. import re


  4. def open_url(url):

  5.     headers = {
  6.         "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64)"
  7.         "AppleWebKit/537.36 (KHTML, like Gecko)"
  8.         "Chrome/90.0.4430.85 Safari/537.36"
  9.         }

  10.     res = requests.get(url,headers=headers)

  11.     return res

  12. def find_movies(res):
  13.    
  14.     soup = bs4.BeautifulSoup(res.text,'html.parser')

  15.     movise = []
  16.     targets = soup.find_all("div",class_="hd")
  17.     for each in targets:
  18.         movies.append(each.a.span.text)


  19.     ranks = []
  20.     targets = soup.find_all("div",class_="bd")
  21.     for each in targets:

  22.         try:
  23.             messages.append(each.p.text.split('\n')[1].strip()+each.p.text.split('\n')[2].strip())
  24.             
  25.         except:
  26.             continue


  27.     result = []
  28.     length = len(movies)
  29.     for i in range(length):
  30.         result.append(movies[i]+ranks[i]+messages[i]+'\n')


  31.     return result


  32. def main():
  33.     host = "https://movie.douban.com/top250"
  34.     res = open_url(host)
  35.     depth = find_depth(res)


  36.     result = []
  37.     for i in range(depth):
  38.         url = host+'/?start='+str(25*i)
  39.         res = open_url(url)
  40.         result.extend(find_movies(res))


  41.     with open("豆瓣TOP250电影.txt","w",encoding="utf-8")as f:
  42.         for each in result:
  43.             f.write(each)


  44. if __name__ == "__main__":

  45.     main()
复制代码



最佳答案
2021-5-22 21:59:36
第53行应该将
  1. depth = find_depth(res)
复制代码

改为
  1. depth =find_movie(res)
复制代码
hjjkikoko.png

最佳答案

查看完整内容

第53行应该将 改为
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-22 21:59:36 | 显示全部楼层    本楼为最佳答案   
第53行应该将
  1. depth = find_depth(res)
复制代码

改为
  1. depth =find_movie(res)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-23 09:17:44 | 显示全部楼层
真的是照着抄吗
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-23 09:49:29 | 显示全部楼层
????不看下代码的吗,定义中根本没有这个方法啊...要不改成find_movies试下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-24 11:17:18 | 显示全部楼层
照着抄都能抄错???
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-25 10:46:33 | 显示全部楼层
错误提示很清楚啊,整个代码里面没有找到这个find_depth方法啊。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-23 13:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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