鱼C论坛

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

[已解决]爬虫遇到的小问题

[复制链接]
发表于 2021-8-1 20:19:08 | 显示全部楼层 |阅读模式

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

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

x
源代码如下:

  1. import requests
  2. from lxml import etree
  3. import os

  4. url = "https://www.ibswtan.com/0/425/"
  5. headers = {
  6.     "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
  7. }

  8. response = requests.get(url,headers=headers)
  9. response.encoding = response.apparent_encoding
  10. html_text = response.text
  11. html_tree = etree.HTML(html_text)
  12. div_list = html_tree.xpath('//div[@id="list"]/dl/dd')
  13. url_list = []
  14. if not os.path.exists("./斗破苍穹"):
  15.     os.mkdir("./斗破苍穹")
  16. for dd in div_list:
  17.     new_url = dd.xpath("./a/@href")[0]
  18.     url_list.append(new_url)

  19. for i in url_list:
  20.     download_url = "https://www.ibswtan.com/0/425/" + i
  21.     # print(download_url)
  22.     r = requests.get(download_url, headers=headers)
  23.     r.encoding = r.apparent_encoding
  24.     detal_html = r.text
  25.     detal_tree = etree.HTML(detal_html)
  26.     book_name = detal_tree.xpath('//div[@class="bookname"]/h1/text()')[0]
  27.     text = detal_tree.xpath('//div[@id="content"]/text()')[0]
  28.     file_path = "./斗破苍穹/" + book_name + ".txt"
  29.     with open(file_path, "w", encoding="utf-8") as f:
  30.         f.write(text)
  31.         print(book_name,'下载完毕')

复制代码



爬虫之后报错:
  1. 四章完毕 下载完毕
  2. 萧炎,我们的主角的图片哦~ 下载完毕
  3. Traceback (most recent call last):
  4.   File "E:/推土机/py-lzy/Requests-bs4-xpath/爬虫-笔趣阁-斗破苍穹.py", line 29, in <module>
  5.     book_name = detal_tree.xpath('//div[@class="bookname"]/h1/text()')[0]
  6. IndexError: list index out of range
复制代码


到报错的位置,我看了【土豆强力推荐——《》网游】这个页面的标题代码是一样的啊,单独爬取能爬取到数据的,为啥会超出索引那
最佳答案
2021-8-1 22:45:53

                               
登录/注册后可看大图

  1. import requests
  2. from lxml import etree
  3. import os
  4. import time


  5. url = "https://www.ibswtan.com/0/425/"
  6. headers = {
  7.     "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
  8. }

  9. response = requests.get(url, headers=headers)
  10. response.encoding = response.apparent_encoding
  11. html_text = response.text
  12. html_tree = etree.HTML(html_text)
  13. div_list = html_tree.xpath('//div[@id="list"]/dl/dd')
  14. url_list = []
  15. if not os.path.exists("./斗破苍穹"):
  16.     # os.mkdir("./斗破苍穹")
  17.     for dd in div_list:
  18.         new_url = dd.xpath("./a/@href")[0]
  19.         url_list.append(new_url)

  20.     for i in url_list:
  21.         download_url = "https://www.ibswtan.com/0/425/" + i
  22.         print(download_url)
  23.         time.sleep(1)  # 睡上1秒就可以了
  24.         r = requests.get(download_url, headers=headers)
  25.         r.encoding = r.apparent_encoding
  26.         detal_html = r.text
  27.         detal_tree = etree.HTML(detal_html)

  28.         book_name = detal_tree.xpath(
  29.             '//div[@class="bookname"]/h1/text()')[0]
  30.         text = detal_tree.xpath('//div[@id="content"]/text()')[0]
  31.         file_path = "./斗破苍穹/" + book_name + ".txt"
  32.         with open(file_path, "w", encoding="utf-8") as f:
  33.             f.write(text)
  34.             print(book_name, '下载完毕')
复制代码

可能是请求的太频繁了,睡上1秒就应该没问题了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-1 21:43:05 From FishC Mobile | 显示全部楼层
[0]都超出范围,明显是一个空列表
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-1 22:23:03 | 显示全部楼层
改了一点你的代码,运行是没有问题,但有一些章节就是爬不了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-1 22:24:25 | 显示全部楼层

                               
登录/注册后可看大图

都是会断了几章
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-1 22:42:55 | 显示全部楼层
我发现了,你好像是被反爬了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-1 22:45:53 | 显示全部楼层    本楼为最佳答案   

                               
登录/注册后可看大图

  1. import requests
  2. from lxml import etree
  3. import os
  4. import time


  5. url = "https://www.ibswtan.com/0/425/"
  6. headers = {
  7.     "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
  8. }

  9. response = requests.get(url, headers=headers)
  10. response.encoding = response.apparent_encoding
  11. html_text = response.text
  12. html_tree = etree.HTML(html_text)
  13. div_list = html_tree.xpath('//div[@id="list"]/dl/dd')
  14. url_list = []
  15. if not os.path.exists("./斗破苍穹"):
  16.     # os.mkdir("./斗破苍穹")
  17.     for dd in div_list:
  18.         new_url = dd.xpath("./a/@href")[0]
  19.         url_list.append(new_url)

  20.     for i in url_list:
  21.         download_url = "https://www.ibswtan.com/0/425/" + i
  22.         print(download_url)
  23.         time.sleep(1)  # 睡上1秒就可以了
  24.         r = requests.get(download_url, headers=headers)
  25.         r.encoding = r.apparent_encoding
  26.         detal_html = r.text
  27.         detal_tree = etree.HTML(detal_html)

  28.         book_name = detal_tree.xpath(
  29.             '//div[@class="bookname"]/h1/text()')[0]
  30.         text = detal_tree.xpath('//div[@id="content"]/text()')[0]
  31.         file_path = "./斗破苍穹/" + book_name + ".txt"
  32.         with open(file_path, "w", encoding="utf-8") as f:
  33.             f.write(text)
  34.             print(book_name, '下载完毕')
复制代码

可能是请求的太频繁了,睡上1秒就应该没问题了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-1 22:46:22 | 显示全部楼层
我这运行正常输出。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-20 22:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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