我在这里尝试做的是获取给定URL的标题,以便我可以确定MIME类型.我希望能够看到是否http://somedomain/foo/会返回HTML文档或JPEG图像.因此,我需要弄清楚如何发送HEAD请求,以便我可以读取MIME类型而无需下载内容.有谁知道这样做的简单方法?
我正在从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) …Run Code Online (Sandbox Code Playgroud) 我查看了请求文档,但我似乎找不到任何东西.我如何只请求标题,以便我可以评估文件大小?