1
我不知道爲什麼下面的代碼在幾次迭代後才停止工作。 python進程將繼續在終端中運行,但沒有文件被下載或檢查。爲什麼我的python進程在幾次迭代之後停止?
有人可以幫忙嗎?由於
#Manga downloader
#!python 3
import requests
import os
import bs4
import time
import urllib.request
import shutil
from pathlib import Path
url='http://www.mangareader.net/'
name=input("Enter comic name:")
chapterstart=int(input("Enter starting chapter number:"))
chapterend=int(input("Enter ending chapter number:"))
nameinput=name.replace(" ","-")
os.makedirs('Documents/Manga/',exist_ok=True)
directorylevel1='Documents/Manga/'+nameinput+'/'
os.makedirs(directorylevel1,exist_ok=True)
a=chapterstart
def imagesave(mangaUrl,filename,directorylevel2):
#res = requests.get(mangaUrl)
#print(res)
#res.raise_for_status()
fulldirectoryname=directorylevel2+filename
mangapath=Path(fulldirectoryname)
if mangapath.exists():
print(fulldirectoryname + ' exists. Moving on.')
else:
print('Downloading image %s...' %(filename))
req = urllib.request.Request(mangaUrl, headers={'User-Agent': 'Mozilla/5.0'})
with urllib.request.urlopen(req) as response: #open(fulldirectoryname,'wb') as outfile:
outfile=open(fulldirectoryname,'wb')
shutil.copyfileobj(response,outfile)
response = None
req = None
#imageFile=open(fulldirectoryname,'wb')
#for chunk in res.iter_content(10000000):
# print(chunk)
# imageFile.write(chunk)
#imageFile.close()
#for chapter 1 to 50
def main():
for a in range(chapterstart,chapterend+1):
b=str(a)
directorylevel2=directorylevel1+b+'/'
os.makedirs(directorylevel2,exist_ok=True)
c=str(a+1)
url='http://www.mangareader.net/'+nameinput+'/'+b
stopurl='http://www.mangareader.net/'+nameinput+'/'+c
res = requests.get(url)
res.raise_for_status
soup=bs4.BeautifulSoup(res.text,"lxml")
mangaElem = soup.select('#imgholder img')
#the imgholder is the div class name then img is the image inside
#print(url)
#for pages 1 to end
while url !=stopurl:
if mangaElem == [ ]:
print('Could not find image')
break
else:
mangaUrl=mangaElem[0].get('src')
filename=mangaElem[0].get('alt')+'.jpg'
imagesave(mangaUrl,filename,directorylevel2)
prevLink=soup.select('#imgholder a')[0]
url='http://www.mangareader.net'+prevLink.get('href')
res = requests.get(url)
res.raise_for_status
soup=bs4.BeautifulSoup(res.text,"lxml")
mangaElem = soup.select('#imgholder img')
#soup = None
a=a+1
print('Done')
if __name__ == "__main__":
main()
你是說,它下載一些漫畫,然後停在兩者之間?如果可以的話,請參考https://github.com/GrayHats/MangaReader。 –
沒有輸出?沒有線停在哪裏?根本沒有預診斷? –
無關提示:逗號之間和操作符之間的空格使代碼更具可讀性。 – Grimmy