鱼C论坛

 找回密码
 立即注册
查看: 2635|回复: 9

[已解决]循环出错

[复制链接]
发表于 2022-12-19 09:19:04 | 显示全部楼层 |阅读模式

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

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

x
作为初学者,模仿教程写出了这段下载漫画的代码,在网站所有页面能正常加载图片的时候正常运行,一旦某一章节的图片失效,就会中断运行
有大神可以帮忙解决跳过出错继续下一循环吗?

import requests
import parsel
import os
import re

resp = requests.get('https://www.xhhm.com/plus/list-20.html').text
selecrot  = parsel.Selector(resp)
url_list = selecrot.css('.detail-list-select li a::attr(href)').getall()
title_list = selecrot.css('.detail-list-select li a::text').getall()
for title,url in zip(title_list,url_list):
        print(f'------------正在下载{title}--------------')
        title = re.sub(r'[\/:*?"<>|.]',"",title)
        if not os.path.exists('./001/'+title):
                os.mkdir('./001/'+title)
        response = requests.get(f'https://www.xhhm.com{url}')
        html_date = response.text
        selector_1 = parsel.Selector(html_date)
        img_list = selector_1.css('.player img[src$=".jpg"]::attr(src)').getall()
        index = 1
        for img in img_list:
                img_date = requests.get(img).content
                with open(f'./001/{title}/{str(index)} .jpg', mode='wb') as f:
                        f.write(img_date)
                print(f'第{str(index)}页,下载成功!!!')
                index += 1
        print(title, '下载成功')
最佳答案
2022-12-19 10:24:36
看不懂代码,但是凭感觉加了几个try-except
  1. import requests
  2. import parsel
  3. import os
  4. import re

  5. resp = requests.get('https://www.xhhm.com/plus/list-20.html').text
  6. print('1')
  7. selecrot  = parsel.Selector(resp)
  8. print('2')
  9. url_list = selecrot.css('.detail-list-select li a::attr(href)').getall()
  10. print('3')
  11. title_list = selecrot.css('.detail-list-select li a::text').getall()
  12. for title,url in zip(title_list,url_list):
  13.     try:
  14.         print(f'------------正在下载{title}--------------')
  15.         title = re.sub(r'[\/:*?"<>|.]',"",title)
  16.         if not os.path.exists('./001/'+title):
  17.             os.mkdir('./001/'+title)
  18.         response = requests.get(f'https://www.xhhm.com{url}')
  19.         html_date = response.text
  20.         selector_1 = parsel.Selector(html_date)
  21.         img_list = selector_1.css('.player img[src$=".jpg"]::attr(src)').getall()
  22.         index = 1
  23.         for img in img_list:
  24.             try:
  25.                 img_date = requests.get(img).content
  26.                 with open(f'./001/{title}/{str(index)} .jpg', mode='wb') as f:
  27.                     f.write(img_date)
  28.                 print(f'第{str(index)}页,下载成功!!!')
  29.                 index += 1
  30.             except:
  31.                 print(f'第{str(index)}页,下载失败')
  32.         print(title, '下载成功')
  33.     except:
  34.         print(title, '下载失败')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-19 09:26:25 | 显示全部楼层
try except
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-12-19 09:53:37 | 显示全部楼层

我找到了可以用这个语句处理,作为一个代码小白,不如何加入到这段代码里,能再具体点吗?谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-19 10:18:16 | 显示全部楼层
crins 发表于 2022-12-19 09:53
我找到了可以用这个语句处理,作为一个代码小白,不如何加入到这段代码里,能再具体点吗?谢谢

哪一条语句出错就放在哪.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-19 10:22:45 | 显示全部楼层
crins 发表于 2022-12-19 09:53
我找到了可以用这个语句处理,作为一个代码小白,不如何加入到这段代码里,能再具体点吗?谢谢

抱歉哈,目前电脑不在手边
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-19 10:24:36 | 显示全部楼层    本楼为最佳答案   
看不懂代码,但是凭感觉加了几个try-except
  1. import requests
  2. import parsel
  3. import os
  4. import re

  5. resp = requests.get('https://www.xhhm.com/plus/list-20.html').text
  6. print('1')
  7. selecrot  = parsel.Selector(resp)
  8. print('2')
  9. url_list = selecrot.css('.detail-list-select li a::attr(href)').getall()
  10. print('3')
  11. title_list = selecrot.css('.detail-list-select li a::text').getall()
  12. for title,url in zip(title_list,url_list):
  13.     try:
  14.         print(f'------------正在下载{title}--------------')
  15.         title = re.sub(r'[\/:*?"<>|.]',"",title)
  16.         if not os.path.exists('./001/'+title):
  17.             os.mkdir('./001/'+title)
  18.         response = requests.get(f'https://www.xhhm.com{url}')
  19.         html_date = response.text
  20.         selector_1 = parsel.Selector(html_date)
  21.         img_list = selector_1.css('.player img[src$=".jpg"]::attr(src)').getall()
  22.         index = 1
  23.         for img in img_list:
  24.             try:
  25.                 img_date = requests.get(img).content
  26.                 with open(f'./001/{title}/{str(index)} .jpg', mode='wb') as f:
  27.                     f.write(img_date)
  28.                 print(f'第{str(index)}页,下载成功!!!')
  29.                 index += 1
  30.             except:
  31.                 print(f'第{str(index)}页,下载失败')
  32.         print(title, '下载成功')
  33.     except:
  34.         print(title, '下载失败')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-19 10:58:51 | 显示全部楼层
网站网址正确?无响应
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-19 11:16:46 | 显示全部楼层
青出于蓝 发表于 2022-12-19 10:58
网站网址正确?无响应

我猜是外网
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-19 11:50:25 | 显示全部楼层
tommyyu 发表于 2022-12-19 10:24
看不懂代码,但是凭感觉加了几个try-except

非常感谢tommyyu,同时也感谢青出于蓝提出建议。
因为本人小白,不懂怎么在语句里从错误点开始循环,所以重新跑了一次。
错误点在20多章,所以耗费的时间有点长,导致现在才回复。现在可以正常的跳过无法获取数据的问题。

网址是我故意漏写的,因为不可描述的原因xhhm8

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

使用道具 举报

发表于 2022-12-19 12:46:56 | 显示全部楼层
学习了,谢谢提供学习的机会
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-17 23:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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