Python 从列表中下载文件(如果尚未下载)
我可以用c#实现,代码相当长 如果有人能告诉我如何通过python实现这一点,那就太酷了 伪代码是:Python 从列表中下载文件(如果尚未下载),python,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
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
虽然我假设
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)
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()
下面是WoLpH针对Python3.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.')
哇,这是惊人的简洁!我求求你看看这些炒作是怎么回事!谢谢你,伙计!使用os.path.basename(url),而不是在“/”上拆分。您应该将路径添加到脚本中。使用PATH=“./downloads”