|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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, '下载成功')
看不懂代码,但是凭感觉加了几个try-except  - import requests
- import parsel
- import os
- import re
- resp = requests.get('https://www.xhhm.com/plus/list-20.html').text
- print('1')
- selecrot = parsel.Selector(resp)
- print('2')
- url_list = selecrot.css('.detail-list-select li a::attr(href)').getall()
- print('3')
- title_list = selecrot.css('.detail-list-select li a::text').getall()
- for title,url in zip(title_list,url_list):
- try:
- 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:
- try:
- 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
- except:
- print(f'第{str(index)}页,下载失败')
- print(title, '下载成功')
- except:
- print(title, '下载失败')
复制代码
|
|