如何停止我的方法在python中运行两次?

码:

import requests
from time import strftime, sleep

a = 20200422
b = 93

def getData(a,b):
    r = requests.get(
        f"https://www.examplesite.com/docs/doc-{str(a)}-testing-str{b}.pdf")
    if r.status_code == 200:
        with open(f"{b}.pdf", 'wb') as f:
            f.write(r.content)
            print(f"File Saved As {b}.pdf")
            retVal = True
            return retVal  
    else:
        print("[!] File Not Found")
        retVal = False
        return retVal

flag = True
i = 0

while flag:
    i += 1
    if(i == 1):
        retVal = True
    else:
        retVal = getData(a,b)
    if(retVal):
        sleep(2)
        a += 1
        b += 1
        getData(a,b)
    else:
        sleep(10)
        getData(a,b)

输出为:

File Saved As 94.pdf
File Saved As 94.pdf
File Saved As 95.pdf
File Saved As 95.pdf
[!] File Not Found
[!] File Not Found

如果网站上没有新文件,我如何下载?

评论