urllib.urlretrieve()
已經這樣做了你。如果輸出文件名存在,它將執行所有必要的檢查以避免再次下載。
但是,只有服務器支持它才能使用。因此,您可能需要打印HTTP標頭(函數調用的第二個結果)以查看是否可以完成緩存。
而且這篇文章可能會有所幫助:http://pymotw.com/2/urllib/
它具有這樣的代碼接近尾聲:
import urllib
import os
def reporthook(blocks_read, block_size, total_size):
if not blocks_read:
print 'Connection opened'
return
if total_size < 0:
# Unknown size
print 'Read %d blocks' % blocks_read
else:
amount_read = blocks_read * block_size
print 'Read %d blocks, or %d/%d' % (blocks_read, amount_read, total_size)
return
try:
filename, msg = urllib.urlretrieve('http://blog.doughellmann.com/', reporthook=reporthook)
print
print 'File:', filename
print 'Headers:'
print msg
print 'File exists before cleanup:', os.path.exists(filename)
finally:
urllib.urlcleanup()
print 'File still exists:', os.path.exists(filename)
此下載文件,顯示進度和打印頭。使用它來調試您的場景,找出爲什麼緩存不能按預期工作。
-1問題是如何確定資源是否在服務器上發生了變化。 –
我的問題並不完全清楚,但提及時間戳文件名稱是 – user2401842
這輸出紀元時間,任何想法如何使其標準(人類可讀)時間/日期? – user2401842