使用urllib.request.urlretrieve从Web复制文件,但未粘贴任何内容

问题描述

我正在尝试使用以下代码从网站复制文件并将其本地保存。但是我的本地驱动程序显示保存的文件为空(即创建的每个excel文件都包含1 KB内存)。有关为何无法正常工作的任何评论

import urllib.request
import pathlib

folder_euronext = "C:/myfolder/"
url_euronext = "https://live.euronext.com/sites/default/files/statistics/factbook/yearly/"
for i in range(2013,2020):
    filename = "euronext_fact_book_"+str(i)+".xls" if i <= 2016 else "euronext_fact_book_"+str(i)+".xlsx"
    print(url_euronext+filename)
    if pathlib.Path(folder_euronext+filename).exists():
        print(filename,"exists...")
    else: 
        urllib.request.urlretrieve(url_euronext+filename,folder_euronext+filename)
        print("copying and pasting",filename)

解决方法

请在下面尝试。您需要安装requests

    #################################################
<#
    CREATE-SHORTCUT - creates shortcut for all files from a source folder
    version       : 1.0
    Author        : 
    Creation Date : 
    Modified Date : 
#>

#------------------------------------------------------------[ variables ]----------------------------------------------------------

$sourceDir="D:\scripts\create-shortcut\source"
$targetDir="D:\scripts\create-shortcut\dest"

#-------------------------------------------------------------[ Script ]-----------------------------------------------------------
# get files/files from folder
$src_gci=Get-Childitem -path $sourceDir -Recurse
$src_files=$src_gci | ? { !$_.PSisContainer }
$src_folders=$src_gci | ? { $_.PSisContainer }

# create subfolders
$src_folders | Copy-Item -Destination { join-path $targetDir $_.Parent.FullName.Substring($sourceDir.Length) } -Force

# create shortcuts
$WshShell = New-Object -comObject WScript.Shell
$src_files | % {
    $lnkName="{0}.lnk" -f $_.BaseName
    $Target = ($_.DirectoryName -replace [regex]::escape($sourceDir),$targetDir) + "\" + $lnkName
    $Shortcut = $WshShell.CreateShortcut($Target)
    $Shortcut.TargetPath = $_.FullName
    $Shortcut.Save()
    # change to SourceFiles ModifiedDate #
    $src_date=$_.LastWriteTime
    Get-ChildItem $Target | % { $_.LastWriteTime = "$src_date" }
}