Как загрузить все папки и файлы внутри URL с помощью Pycurl или запросить библиотеку в Python? - PullRequest
0 голосов
/ 05 апреля 2019

Я должен загрузить все файлы в URL на мой локальный сервер.Я написал фрагмент кода для загрузки с помощью pycurl

def download(url, outputPath):
    fileName = get_file_name(url)
    logger.info("url: %s" %(url))
    print("url: ", url)
    curl = pycurl.Curl()
    curl.setopt(pycurl.CAINFO, certifi.where())
    curl.setopt(pycurl.URL, url)
    curl.setopt(pycurl.NOPROGRESS, 0)
    curl.setopt(pycurl.PROGRESSFUNCTION, downloadProgress)
    curl.setopt(pycurl.FOLLOWLOCATION, 1)
    curl.setopt(pycurl.MAXREDIRS, 5)
    curl.setopt(pycurl.CONNECTTIMEOUT, 50)
    curl.setopt(pycurl.TIMEOUT, timeOut)
    curl.setopt(pycurl.FTP_RESPONSE_TIMEOUT, 600)
    curl.setopt(pycurl.NOSIGNAL, 1)
    curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)
    curl.setopt(pycurl.USERPWD, "Vthamburaj" + ':' + "Vicky@1987")

    if os.path.exists(os.path.join(outputPath, fileName)):
        curl.fp = open(os.path.join(outputPath, fileName), "ab")
        curl.setopt(pycurl.RESUME_FROM, os.path.getsize(os.path.join(outputPath, fileName)))
    else:
        curl.fp = open(os.path.join(outputPath, fileName), "wb")

    curl.setopt(pycurl.WRITEDATA, curl.fp)

    try:
        print("Start time: " + time.strftime("%c"))
        logger.debug("Download start time: " + time.strftime("%c"))

        curl.perform()

        print("\nTotal-time: " + str(curl.getinfo(curl.TOTAL_TIME)))
        print("Download speed: %.2f bytes/second" % (curl.getinfo(curl.SPEED_DOWNLOAD)))
        print("Document size: %d bytes" % (curl.getinfo(curl.SIZE_DOWNLOAD)))
        logger.debug("Total-time: " + str(curl.getinfo(curl.TOTAL_TIME)))
        logger.debug("Download speed: %.2f bytes/second" % (curl.getinfo(curl.SPEED_DOWNLOAD)))
        logger.debug("Document size: %d bytes" % (curl.getinfo(curl.SIZE_DOWNLOAD)))
    except:
        logger.error("download failed - ", exc_info=True)
        print("download failed!!! check log for detailed message")

    curl.close()
    curl.fp.close()
    sys.stdout.flush()

. Это прекрасно работает, если в URL есть файл, например "my host name" /file/1.zip.

Но для моего случая в моей ссылке много файлов и папок, например, url (https://cms.myhost.info/share/page/site/CPA/documentlibrary#filter=path%7C2010-Mar%2F26%2F9781435483781).) И я хочу загрузить их все в той же структуре папок на свой локальный диск.

Как это сделать с помощью Python?

...