如何使用Python有效地下载文件?

时间:2019-05-06 08:57:10

标签: python download

我要下载文件。

我使用了asyncio,但是下载文件时很长时间没有响应。此外,当服务器证书无效时,还会出现问题。

这是我的代码:

socket.setdefaulttimeout(30)
async def get_file(url, folder, session):
    filename = folder.strip('\\') + '\\' + str(datetime.datetime.now()).replace(':', '_') + '.pdf'
    async with session.get(url, headers = headers) as resp:
        if resp.status == 200:
            f = await aiofiles.open(filename, mode='wb')
            await f.write(await resp.read())
            await f.close()

async def download_pdf(urls, folder):
    connector = aiohttp.TCPConnector(limit=60)
    semaphore = asyncio.Semaphore(10)
    async with semaphore:
        async with ClientSession(connector=connector, headers=headers) as session:
            tasks = [get_file(url, folder, session) for url in urls]
            result = await asyncio.gather(*tasks)

urls = ['https://www.example.com/abc.pdf', 'https://www.example2.com/def.pdf']
directory = r'C:\data'
asyncio.run(download_pdf(urls, directory))

如何 下载 文件 有效

1 个答案:

答案 0 :(得分:0)

使用requests库,您可以将响应流式传输到文件中:

with open(r"/path/to/file", 'w+b') as f:
    for chunk in requests.get(yourURL, stream=True).iter_content(chunk_size=1024):
        f.write(chunk)