wget - 使用python的内容配置

时间:2018-05-09 19:16:16

标签: python cmd wget

我正在尝试使用python从网站下载内容。我在使用

工作的批处理文件的Windows TestRecurse中有一些代码
TestRecurse\b.txt
TestRecurse\dir1
TestRecurse\dir1\a.txt

我希望能够在python脚本中执行此操作。我怎样才能做到这一点?到目前为止我已经

Get-ChildItem 'C:\tmp\TestRecurse\' | `    # Get the list of items within TestRecurse
   ? {$_.PSIsContainer} | `                # Filter items that are folders
   % {Get-ChildItem $_.FullName -Recurse}  # Iterate through and get all items within those folders

我收到错误 cmd。我认为这与没有wget --content-disposition "url" 参数有关。 python wget模块中是否存在与for name,ID in stations[stationid==26887]: count=0 for y in year: for m in month: url = "http://climate.weather.gc.ca/climate_data/bulk_data_e.html?format=csv&stationID="+str(ID)+"&Year="+str(y)+"&Month="+str(m)+"&Day=14&timeframe=1&submit= Download+Data" print(url) urllib.request.urlretrieve(url, str(count)+".csv") count=count+1 中的内容匹配的参数?如果我只是将网址复制并粘贴到网络浏览器中,它会立即开始下载带有自己唯一名称的.csv文件。不知道为什么这不能通过python工作。这是我打印我的网址时得到的:     http://climate.weather.gc.ca/climate_data/bulk_data_e.html?format=csv&stationID=26887&Year=2018&Month=8&Day=14&timeframe=1&submit=下载+数据

2 个答案:

答案 0 :(得分:1)

尝试使用urllib.request

import urllib.request
url = '<your url>'
urllib.request.urlretrieve(url, 'file.ext')

现在你的文件应该在python的当前工作目录中。

您可以找到当前的工作目录:

import os
os.getcwd()
# 'C:\\Python' would similar to the output if you are on windows

如果您想要一个download函数将文件名默认为下载文件的名称,您可以像这样定义此函数。

def download(url, filename=url.split('/')[-1]):
    return urllib.request.urlretrieve(url, filename)
# This should download a file into your current working directory
download(url)

答案 1 :(得分:0)

python wget根本不是wget

从他们的文档中说明

  

wget.py与Unix wget实用程序不兼容,   为新人制作直观的命令行界面。

@nanomosfet有一个很好的答案

如果您使用wget设置,则可以使用子进程模块调用真实的wget

相关问题