繁体   English   中英

从 URL 下载文件并将其保存在 Python 文件夹中

[英]Download file from URL and save it in a folder Python

我有很多文件类型为.docx.pdf的 URL 我想运行一个 python 脚本,从 URL 下载它们并将其保存在一个文件夹中。 这是我为单个文件所做的工作,我会将它们添加到 for 循环中:

response = requests.get('http://wbesite.com/Motivation-Letter.docx')
with open("my_file.docx", 'wb') as f:
    f.write(response.content)

但它保存的my_file.docx只有 266 字节并且已损坏,但 URL 没问题。

更新:

添加了此代码并且它可以工作,但我想将它保存在一个新文件夹中。

import os
import shutil
import requests

def download_file(url, folder_name):
    local_filename = url.split('/')[-1]
    path = os.path.join("/{}/{}".format(folder_name, local_filename))
    with requests.get(url, stream=True) as r:
        with open(path, 'wb') as f:
            shutil.copyfileobj(r.raw, f)

    return local_filename

尝试使用选项:

import os
import requests


def download(url: str, dest_folder: str):
    if not os.path.exists(dest_folder):
        os.makedirs(dest_folder)  # create folder if it does not exist

    filename = url.split('/')[-1].replace(" ", "_")  # be careful with file names
    file_path = os.path.join(dest_folder, filename)

    r = requests.get(url, stream=True)
    if r.ok:
        print("saving to", os.path.abspath(file_path))
        with open(file_path, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024 * 8):
                if chunk:
                    f.write(chunk)
                    f.flush()
                    os.fsync(f.fileno())
    else:  # HTTP status code 4XX/5XX
        print("Download failed: status code {}\n{}".format(r.status_code, r.text))


download("http://website.com/Motivation-Letter.docx", dest_folder="mydir")

请注意,上面示例中的mydir当前工作目录中的文件夹名称。 如果mydir不存在,脚本将在当前工作目录中创建它并将文件保存在其中。 您的用户必须有权在当前工作目录中创建目录和文件。

您可以在dest_folder传递绝对文件路径,但请先检查权限。

PS:避免在一篇文章中提出多个问题

尝试:

import urllib.request 
urllib.request.urlretrieve(url, filename)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM