2010-07-04 78 views
4

我可以在c#中做到这一点,代码很长。如果还没有下载,从列表中下载文件

如果有人能告诉我这将如何通过python完成会很酷。

的伪码:

url: www.example.com/somefolder/filename1.pdf 

1. load file into an array (file contains a url on each line) 
2. if file e.g. filename1.pdf doesn't exist, download file 

该脚本可以在以下布局:

/python-downloader/ 
/python-downloader/dl.py 
/python-downloader/urls.txt 
/python-downloader/downloaded/filename1.pdf 

回答

11

这应该做的伎俩,虽然我假设urls.txt文件只包含的URL。不是前缀url:

import os 
import urllib 

DOWNLOADS_DIR = '/python-downloader/downloaded' 

# For every line in the file 
for url in open('urls.txt'): 
    # Split on the rightmost/and take everything on the right side of that 
    name = url.rsplit('/', 1)[-1] 

    # Combine the name and the downloads directory to get the local filename 
    filename = os.path.join(DOWNLOADS_DIR, name) 

    # Download the file if it does not exist 
    if not os.path.isfile(filename): 
     urllib.urlretrieve(url, filename) 
+0

哇,这是惊人的简洁!我乞求看看所有的炒作是关于什么!谢啦! – Blankman 2010-07-04 01:15:35

+0

使用os.path.basename(url)而不是在'/'上分割。 – TravisThomas 2017-05-07 18:36:10

2

这是较少的代码在Python中,你可以使用这样的事情:

import urllib2 
improt os 

url="http://.../" 
# Translate url into a filename 
filename = url.split('/')[-1] 

if not os.path.exists(filename) 
    outfile = open(filename, "w") 
    outfile.write(urllib2.urlopen(url).read()) 
    outfile.close() 
4

这里是WoLpH的脚本为Python 3.3略加修改。

#!/usr/bin/python3.3 
import os.path 
import urllib.request 

links = open('links.txt', 'r') 
for link in links: 
    link = link.strip() 
    name = link.rsplit('/', 1)[-1] 
    filename = os.path.join('downloads', name) 

    if not os.path.isfile(filename): 
     print('Downloading: ' + filename) 
     try: 
      urllib.request.urlretrieve(link, filename) 
     except Exception as inst: 
      print(inst) 
      print(' Encountered unknown error. Continuing.')