鱼C论坛

 找回密码
 立即注册
查看: 1065|回复: 4

[已解决]求大佬看下哪里有问题

[复制链接]
发表于 2019-4-2 09:51:18 | 显示全部楼层 |阅读模式
2鱼币
报错:IndexError: list index out of range 为什么?

  1. import requests
  2. import bs4
  3. import time
  4. import sys
  5. import openpyxl


  6. def open_url(url):
  7.     proxies = {"http": "119.179.172.243:8060", "https": "124.167.248.230:3128"}
  8.     headers = {
  9.         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
  10.     }
  11.     req = requests.get(url, headers=headers, proxies=proxies)
  12.     return req


  13. def find_books(req):
  14.     bs = bs4.BeautifulSoup(req.text, "html.parser")

  15.     # 书名
  16.     booknames = []
  17.     books = bs.find_all("div", class_="info")
  18.     for each in books:
  19.         booknames.append(each.h2.a.text.strip().replace(" ", "").replace("\n", ""))
  20.     # print(booknames)
  21.     # 评分
  22.     scores = []
  23.     score = bs.find_all("span", class_="rating_nums")
  24.     for each in score:
  25.         scores.append(each.text)
  26.     # print(scores)

  27.     # 出版信息
  28.     pubs = []
  29.     pub = bs.find_all("div", class_="pub")
  30.     for each in pub:
  31.         pubs.append(each.text.strip().replace("\n", ""))
  32.     # print(pubs)

  33.     # url
  34.     urls = []
  35.     url = bs.find_all("div", class_="info")
  36.     for each in url:
  37.         href = each.find_all('a')
  38.         urls.append(href[0].get('href'))
  39.     # print(urls)

  40.     result = []
  41.     length = len(booknames)
  42.     for i in range(length):      
  43.         result.append([booknames[i], scores[i], pubs[i], urls[i]])        
  44.     # print(result)
  45.     return result

  46.     # 查页数


  47. def find_pages(req):
  48.     bs = bs4.BeautifulSoup(req.text, 'html.parser')
  49.     page = bs.find("span", class_="next").previous_sibling.previous_sibling.text
  50.     return int(page)


  51. def save_to_excel(result):
  52.     wb = openpyxl.Workbook()
  53.     ws = wb.active
  54.     ws.append(['书名', '评分', '出版信息', '相关链接'])
  55.     for each in result:
  56.         ws.append(each)
  57.     wb.save("豆瓣图书.xlsx")


  58. def main():
  59.     host = "https://book.douban.com/tag/%E7%BC%96%E7%A8%8B"
  60.     res = open_url(host)
  61.     depth = find_pages(res)
  62.     result = []
  63.     for i in range(depth):
  64.         url = host + "?start=" + str(i * 20) + "&type=T"
  65.         req = open_url(url)
  66.         result.extend(find_books(req))
  67.         now_jd = ((i + 1) / depth) * 100
  68.         sys.stdout.write(
  69.             "\r爬取中 %d%% [%s%s] 第%d页" % (now_jd, '>' * int((now_jd) / 2), '-' * int((100 - now_jd) / 2),(i + 1)))
  70.         sys.stdout.flush()
  71.         time.sleep(1.5)
  72.     print("爬取完成!")
  73.     print(result)

  74.     '''with open("豆瓣图书.txt", "w", encoding="utf-8")as f:
  75.             for each in result:
  76.             f.write(each)'''

  77.     save_to_excel(result)

  78. if __name__ == '__main__':
  79.     main()
复制代码
最佳答案
2019-4-2 09:51:19
那不用想了,你的列表是空的
你之前就没添加上元素,你用审查元素得到的渲染后代码和源代码不一致,你去源代码看看什么格式,有的可能还通过JS函数等等

最佳答案

查看完整内容

那不用想了,你的列表是空的 你之前就没添加上元素,你用审查元素得到的渲染后代码和源代码不一致,你去源代码看看什么格式,有的可能还通过JS函数等等
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-2 09:51:19 | 显示全部楼层    本楼为最佳答案   
那不用想了,你的列表是空的
你之前就没添加上元素,你用审查元素得到的渲染后代码和源代码不一致,你去源代码看看什么格式,有的可能还通过JS函数等等
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-2 09:55:05 | 显示全部楼层
出错贴全了直接去那行找,一般就是你的索引设置不对,或者直接你的列表就是空的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-4-2 09:57:21 | 显示全部楼层
塔利班 发表于 2019-4-2 09:55
出错贴全了直接去那行找,一般就是你的索引设置不对,或者直接你的列表就是空的
  1. File "E:/pycharmproject/demo1/doubanbook.py", line 97, in <module>
  2.     main()
  3.   File "E:/pycharmproject/demo1/doubanbook.py", line 81, in main
  4.     result.extend(find_books(req))
  5.   File "E:/pycharmproject/demo1/doubanbook.py", line 51, in find_books
  6.     result.append([bookNames[i], scores[i], pubs[i], urls[i]])
  7. IndexError: list index out of range
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-4-2 10:02:20 | 显示全部楼层
塔利班 发表于 2019-4-2 10:00
那不用想了,你的列表是空的
你之前就没添加上元素,你用审查元素得到的渲染后代码和源代码不一致,你去源 ...

明白了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-1 02:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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