美文网首页
Python下载文件处理

Python下载文件处理

作者: python测试开发 | 来源:发表于2019-02-16 22:33 被阅读48次

在本教程中,您将学习如何使用不同的Python模块从Web下载文件。 您将下载常规文件,网页,YouTube视频,Google网盘文件,Amazon S3其他来源的文件。

此外,您将学习如何克服许多可能遇到的挑战,例如下载重定向文件,下载大文件,多线程下载和其他策略。

requests

图片.png
import requests

url = 'https://www.python.org/static/img/python-logo@2x.png'
myfile = requests.get(url)
open('PythonImage.png', 'wb').write(myfile.content)

重定向

import requests

url = 'https://readthedocs.org/projects/python-guide/downloads/pdf/latest/'
myfile = requests.get(url, allow_redirects=True)
open('hello.pdf', 'wb').write(myfile.content)

分块下载大文件

import requests

url = 'https://www.cs.uky.edu/~keen/115/Haltermanpythonbook.pdf'
r = requests.get(url, stream = True)
with open("PythonBook.pdf", "wb") as Pypdf:
    for chunk in r.iter_content(chunk_size = 1024):
        if chunk:
            Pypdf.write(chunk)

参考资料

相关文章

网友评论

      本文标题:Python下载文件处理

      本文链接:https://www.haomeiwen.com/subject/gnxweqtx.html