2017-07-03 128 views
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() 
+0

你是说,它下载一些漫画,然后停在两者之间?如果可以的话,请参考https://github.com/GrayHats/MangaReader。 –

+1

没有输出?没有线停在哪里?根本没有预诊断? –

+4

无关提示:逗号之间和操作符之间的空格使代码更具可读性。 – Grimmy

回答

0

财产以后考虑:

你的代码运行平了 - 也就是说,它从网站抓住保持在文件计算机的速度。该站点希望以人为速度抓取文件 - 如果它能够更快地检测到任何内容,则可能会在几个文件后阻止访问。我看到你输入了time,但没有使用它 - 也许你打算在访问之间添加一个time.sleep(1)以防止被锁定。

几年前,我偶尔会被要求在访问速度过快时手动阻止来自在线科学数据库的网站。我知道政府数据库记录了访问速度(每秒页面数),低于该数据的数量,程序应该保留以避免被阻止。

做个好网友。值得一试。

相关问题