如果尚未下载,则从列表中下载文件

时间:2010-07-04 01:05:15

标签: python

我可以在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

3 个答案:

答案 0 :(得分:14)

这应该可以解决问题,尽管我假设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)

答案 1 :(得分:7)

这是对Python 3.3的WoLpH脚本的略微修改版本。

#!/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.')

答案 2 :(得分:3)

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()