在Jupyter中下载文件:Windows上的wget?

时间:2019-09-01 18:58:52

标签: jupyter-notebook jupyter-lab

我正在尝试从网址下载文件。

我发现wget命令能够做到这一点。由于我使用Jupyter,所以我不想使用pip,但是conda install conda wget无效,因为默认存储库中没有Windows wget。因此,我做了conda install menpo wget,成功安装了wget。但是,我仍然无法在JupyterLab中导入wgetModuleNotFoundError: No module named 'wget'

  1. 是否还有其他导入wget的步骤?
  2. 是否有更好的方式在Jupyter中下载文件?

2 个答案:

答案 0 :(得分:3)

在 Google-Collab 中,此代码运行良好:

!wget https://audio-previews.elements.envatousercontent.com/files/6319559/preview.mp3 -O sample_f.mp3
!wget https://audio-previews.elements.envatousercontent.com/files/256324900/preview.mp3 -O sample_m.mp3

但不在 Windows 10 本地。在我的 Jupyter Notebook 单元中,此代码:

# pip install wget
!python -m wget https://audio-previews.elements.envatousercontent.com/files/6319559/preview.mp3 -o sample_f.mp3
!python -m wget https://audio-previews.elements.envatousercontent.com/files/256324900/preview.mp3 -o sample_m.mp3

保存文件并返回:

Saved under sample_f.mp3
Saved under sample_m.mp3

或者我尝试了另一种方法:我从 "gcloud iot devices list" command 下载了适用于 Windows 的 wget.exe 并将其移动到 PATH 目录。然后代码也可以工作:

!wget https://audio-previews.elements.envatousercontent.com/files/6319559/preview.mp3 -O sample_f.mp3
!wget https://audio-previews.elements.envatousercontent.com/files/256324900/preview.mp3 -O sample_m.mp3

答案 1 :(得分:0)

正如Trenton_M所指出的,有一个urlib库可以代替wget来做到这一点:

import urllib.request
url-variable = 'https://adress'
urllib.request.urlretrieve(url, filename-variable)