我正在從Web伺服器下載整個目錄.它工作正常,但我無法弄清楚如何在下載之前獲取檔案大小,以比較它是否在伺服器上更新.這可以像我從FTP伺服器下載檔案一樣完成嗎?
import urllib
import re
url = "http://www.someurl.com"
# Download the page locally
f = urllib.urlopen(url)
html = f.read()
f.close()
f = open ("temp.htm", "w")
f.write (html)
f.close()
# List only the .TXT / .ZIP files
fnames = re.findall('^.*<a href="(\w+(?:\.txt|.zip)?)".*$', html, re.MULTILINE)
for fname in fnames:
print fname, "..."
f = urllib.urlopen(url + "/" + fname)
#### Here I want to check the filesize to download or not ####
file = f.read()
f.close()
f = open (fname, "w")
f.write (file)
f.close()
@Jon:感謝您的快速答案.它有效,但Web伺服器上的檔案大小略低於下載檔案的檔案大小.
例如:
Local Size Server Size
2.223.533 2.115.516
664.603 662.121
它與CR/LF轉換有什麼關係?