鱼C论坛

 找回密码
 立即注册
查看: 6422|回复: 6

[已解决]关于爬豆瓣TOP250电影的问题 IndexError: list index out of range

[复制链接]
发表于 2018-4-29 02:31:15 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 helloTOM 于 2018-5-6 21:55 编辑

简单浏览了一下小甲鱼的爬豆瓣课程  没看小怎么看甲鱼写的代码自己写的渣代码  日常出问题 不过没关系 代码贴上 希望指点

最新更新(5.4)  虽然遇到了比较多的问题  还是自己解决了(原代码看似清晰有条理  不过过于冗杂  爬取一页过多的调用了url(我加了个测试代码  爬一页大概要调用url 50-100次 我的天!!!   豆瓣不把我封了才怪)后来将原来爬的电影名字 信息 评分 简热评 集成到了一个函数 这下可以了 豆瓣服务器君不封我的ip了  我测试了下  爬十页调用url10次  不过又出现了错误:IndexError: list index out of range   WTF!!!!!!!!!!什么情况???? 原来我爬的简热评里有两部电影没有简热评。。。。才会出现这类情况   于是加了简单的渣代码便可以如期运行了!!!!!!(代码见评论区)

更新一波:今天我又试了一下我的代码居然可以爬到第五页(平时都是到第四页就报这个错误 不过今天报错后我浏览豆瓣网页是直接说403。。。平时都是:检测到有异常请求从你的 IP 发出,请 登录 使用豆瓣。我觉得我的代码应该没有问题,问题应该是豆瓣那边给我禁了  过会用一下代理IP池再试试(反正也不知道还有什么好办法了。。。。 ))
  1. import requests
  2. import bs4

  3. def open_url(url):
  4.     headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"}
  5.     res = requests.get(url,headers=headers)
  6.     soup = bs4.BeautifulSoup(res.text,"html.parser")
  7.     return soup
  8.    
  9. def movices_name(url):
  10.     name = []
  11.     targets = open_url(url).find_all("div",class_="hd")
  12.     for each in targets:
  13.         name.append(each.a.span.text)
  14.     return name

  15. def movices_information(url):
  16.     information = []
  17.     targets = open_url(url).find_all("div",class_="bd")
  18.     for each in targets:
  19.         information.append(each.p.text)
  20.     del information[0]
  21.     return information

  22. def movices_score(url):
  23.     score = []
  24.     targets = open_url(url).find_all("span",class_="rating_num")
  25.     for each in targets:
  26.         score.append(each.text)
  27.     return score

  28. def movices_hotcomment(url):
  29.     hotcomment = []
  30.     targets = open_url(url).find_all("span",class_="inq")
  31.     for each in targets:
  32.         hotcomment.append(each.text)
  33.     return hotcomment

  34. def download_top(url):
  35.     result = []
  36.     numbers = len(movices_name(url))
  37.     for i in range(numbers):
  38.         result.extend("                        "+movices_name(url)[i]+"  "+movices_information(url)[i]+"    "+movices_score(url)[i]+"  "+movices_hotcomment(url)[i]+"\n\n")

  39.     with open("豆瓣TOP250电影一览.txt","a",encoding = "utf-8") as f:
  40.         for each in result:
  41.             f.write(each)
  42.       
  43.         
  44. def main():
  45.     count = 0
  46.     while count<10:
  47.         print("正在爬取第%s页" %str(count+1))

  48.         url = "https://movie.douban.com/top250" + "?start="+str(25 * count)
  49.         
  50.         download_top(url)
  51.         
  52.         count +=1
  53.         
  54.         print("已经爬取第%s页" %str(count))
  55.         


  56. if __name__ == "__main__":
  57.     main()

复制代码

两个问题:
1.程序报错不知道怎么解决  报错为:line 47, in download_top
    result.extend("                        "+movices_name(url)+"  "+movices_information(url)+"    "+movices_score(url)+"  "+movices_hotcomment(url)+"\n\n")
IndexError: list index out of range
2.感觉我的代码运行好慢啊 我事后试了一下小甲鱼老师的代码感觉他的代码运行的挺快的。  我想知道我们的代码我对比了一下差别不算大啊  为啥效率不一样呢?
最佳答案
2018-4-29 07:02:28
报错为列表取值超出index范围了,应该是你的哪个索引没有取到值
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-29 07:02:28 | 显示全部楼层    本楼为最佳答案   
报错为列表取值超出index范围了,应该是你的哪个索引没有取到值
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-29 11:51:57 | 显示全部楼层
galaxybamboo 发表于 2018-4-29 07:02
报错为列表取值超出index范围了,应该是你的哪个索引没有取到值

应该不是吧  因为我用我自己写的代码都可以正常爬取前三页  不知道为什么到第四页就报错了 而且报错后我打开豆瓣TOP250就告诉我:检测到有异常请求从你的 IP 发出,请 登录 使用豆瓣。 而小甲鱼老师的代码就完全没有任何问题 不知道问什么呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-29 16:10:46 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-4-29 22:32:02 | 显示全部楼层
等待ing
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-30 17:18:44 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-5-4 21:09:27 | 显示全部楼层
  1. import requests
  2. import bs4

  3. def open_url(url):
  4.     headers = {
  5.         "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
  6.         "Referer":"https://movie.douban.com/top250"
  7.         }
  8.     res = requests.get(url,headers=headers)
  9.     soup = bs4.BeautifulSoup(res.text,"html.parser")
  10.     #print("服务器君要爆炸了!!!!!")
  11.     return soup
  12.    
  13. def movices_name_information_score_hotcomment(soup,count):
  14.     name = []
  15.     information = []
  16.     score = []
  17.     hotcomment = []
  18.     result = []

  19.   
  20.     movice_name = soup.find_all("div",class_="hd")
  21.     for each in movice_name:
  22.         name.append(each.a.span.text.strip())
  23.      
  24.     movice_information = soup.find_all("div",class_="bd")
  25.     for each in movice_information:
  26.         information.append(each.p.text.strip())
  27.     del information[0]

  28.     movice_score = soup.find_all("span",class_="rating_num")
  29.     for each in movice_score:
  30.         score.append(each.text.strip())
  31.         
  32.     movice_hotcomment = soup.find_all("span",class_="inq")
  33.     for each in movice_hotcomment:
  34.         hotcomment.append(each.text.strip())

  35.     if count==7:
  36.         hotcomment.insert(14,"无简评")

  37.     if count==9:
  38.         hotcomment.insert(22,"无简评")
  39.         
  40.    
  41.     numbers = len(name)
  42.     for i in range(numbers):
  43.         result.extend(name[i]+information[i]+score[i]+"\n"+hotcomment[i]+"\n\n")

  44.     return result


  45. def download_top(result):
  46.     with open("豆瓣TOP250电影一览.txt","a",encoding = "utf-8") as f:
  47.         for each in result:
  48.             f.write(each)
  49.       
  50.         
  51. def main():
  52.     count = 0
  53.     while count<10:
  54.         print("正在爬取第%s页" %str(count+1))

  55.         url = "https://movie.douban.com/top250" + "?start="+str(25 * count)

  56.         soup = open_url(url)

  57.         result = movices_name_information_score_hotcomment(soup,count)
  58.         
  59.         download_top(result)
  60.         
  61.         count +=1
  62.         
  63.         print("已经爬取第%s页" %str(count))


  64. if __name__ == "__main__":
  65.     main()

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 13:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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