鱼C论坛

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

一个列表推导式的错误(代码内含爬虫)

[复制链接]
发表于 2021-8-17 15:39:50 | 显示全部楼层 |阅读模式

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

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

x
代码:
  1. from bs4 import BeautifulSoup
  2. import requests as re
  3. headers={}
  4. headers["User-Agent"]="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.73"
  5. web = None
  6. soup = None
  7. page_url = ["https://movie.douban.com/top250"]
  8. def fulsh(url_name=''):
  9.     global web,soup
  10.     web = re.get(url_name,headers=headers)
  11.     soup = BeautifulSoup(web.text,"html.parser")
  12. def find():
  13.     a=soup.find_all("div",class_="hd")
  14.     name=[x.a.span.text for x in [x for x in a]]
  15.     b = soup.find_all("div",class_="bd")
  16.     writer = [x.p.text.strip()\
  17.               .replace("\n",'').replace("\t",'').replace(" ",'').replace("\xa0",'')\
  18.               for x in [x for x in b]][1:]
  19.     c = soup.find_all("p",class_="quote")
  20.     world = [x.span.text for x in [x for x in c]]
  21.     b = (name,writer,world)
  22.     return [(name[x]+' ',writer[x]+' ',world[x]) for x in range(len(name))]
  23.     #return [(b[0][x]+' ',b[1][x]+' ',b[2][x]+' ') for x in range(len(b[0]))]


  24. def main():
  25.     global page_url
  26.     return_=[]
  27.     fulsh(page_url[0])
  28.     page_url.extend([''.join(["https://movie.douban.com/top250",x.attrs['href']]) for x in soup.find_all("div",class_="paginator")[0].find_all("a")])
  29.     for i in page_url:
  30.         fulsh(i)
  31.         return_.extend(find())
  32.     retrun_='\n'.join(return_)
  33.     return return_
  34. if __name__ == '__main__':
  35.     main()
复制代码

报错代码:
  1. Traceback (most recent call last):
  2.   File "C:/Users/中维电器维修/Desktop/top250.py", line 37, in <module>
  3.     main()
  4.   File "C:/Users/中维电器维修/Desktop/top250.py", line 33, in main
  5.     return_.extend(find())
  6.   File "C:/Users/中维电器维修/Desktop/top250.py", line 22, in find
  7.     return [(name[x]+' ',writer[x]+' ',world[x]) for x in range(1,23)]
  8.   File "C:/Users/中维电器维修/Desktop/top250.py", line 22, in <listcomp>
  9.     return [(name[x]+' ',writer[x]+' ',world[x]) for x in range(1,23)]
  10. IndexError: list index out of range
复制代码

本人python版本是3.8和3.9
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-17 15:56:47 | 显示全部楼层
列表超出索引了。
  1. return [(name[x]+' ',writer[x]+' ',world[x]) for x in range(len(name))]
复制代码

索引用的全部是x?  是否长度会不一样?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-17 16:18:32 | 显示全部楼层
最基本的排错方法,发现哪个列表索引超出范围
就在生成该列表之后,使用该列表之前  print一下,看看是否是空列表  或者真的超索引了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-17 16:29:18 | 显示全部楼层
wp231957 发表于 2021-8-17 16:18
最基本的排错方法,发现哪个列表索引超出范围
就在生成该列表之后,使用该列表之前  print一下,看看是否 ...

不是,列表没问题,都是25,我自己导出来之后就没问题,但在函数里执行就有问题,不信你自己试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-20 01:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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