Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的python进程在几次迭代后就停止了?_Python_Python 3.x - Fatal编程技术网

为什么我的python进程在几次迭代后就停止了?

为什么我的python进程在几次迭代后就停止了?,python,python-3.x,Python,Python 3.x,我不知道为什么下面的代码在几次迭代后就停止运行了。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

我不知道为什么下面的代码在几次迭代后就停止运行了。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()
值得考虑的是:

您的代码运行完全正常——也就是说,它一直以计算机速度从站点抓取文件。该网站希望文件能以人的速度抓取——如果它检测到任何更快的文件,它可能会在几个文件之后阻止访问。我看到您导入了
time
,但没有使用它——也许您是想在访问之间添加
time.sleep(1)
,以防止被锁定

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

做一个好网民。值得一试。

需要考虑的事项:

您的代码运行完全正常——也就是说,它一直以计算机速度从站点抓取文件。该网站希望文件能以人的速度抓取——如果它检测到任何更快的文件,它可能会在几个文件之后阻止访问。我看到您导入了
time
,但没有使用它——也许您是想在访问之间添加
time.sleep(1)
,以防止被锁定

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


做一个好网民。值得一试。

你是说,它下载了一些漫画,然后停在中间?试着参考这个:如果可以的话。没有输出?没有线路在哪里停?完全没有预先诊断?不相关的提示:逗号后的空格和运算符之间的空格使代码更具可读性。你的意思是,它下载一些漫画,然后在两者之间停止?试着参考这个:如果可以的话。没有输出?没有线路在哪里停?完全没有预诊断?不相关提示:逗号后和运算符之间的空格使代码更具可读性。