仅当服务器副本比本地副本更新时,从服务器下载新文件的标准pythonic方法是什么?
我今天的python-search-fu非常弱,或者确实需要像下面那样推出自己的日期时间解析器和比较器。真的没有requests.header.get_datetime_object('last-modified')
吗?还是request.save_to_file(url, outfile, maintain_datetime=True)
?
import requests
import datetime
r = requests.head(url)
url_time = r.headers['last-modified']
file_time = datetime.datetime.fromtimestamp(os.path.getmtime(dstFile))
print url_time #emits 'Sat, 28 Mar 2015 08:05:42 GMT' on my machine
print file_time #emits '2015-03-27 21:53:28.175072'
if time_is_older(url_time, file_time):
print 'url modtime is not newer than local file, skipping download'
return
else:
do_download(url)
os.utime(dstFile, url_time) # maintain server's file timestamp
def time_is_older(str_time, time_object):
''' Parse str_time and see if is older than time_object.
This is a fragile function, what if str_time is in different locale?
'''
parsed_time = datetime.datetime.strptime(str_time,
#Fri, 27 Mar 2015 08:05:42 GMT
'%a, %d %b %Y %X %Z')
return parsed_time < time_object
答案 0 :(得分:4)
import requests
import datetime
from dateutil.parser import parse as parsedate
r = requests.head(url)
url_time = r.headers['last-modified']
url_date = parsedate(url_time)
file_time = datetime.datetime.fromtimestamp(os.path.getmtime(dstFile))
if url_date > file_time :
donwload it !
答案 1 :(得分:1)
我使用了以下代码,该代码还考虑了时区,并确保两个datetime对象都知道。
import datetime
import requests
from dateutil.parser import parse as parsedate
r = requests.head(url)
url_datetime = parsedate(r.headers['Last-Modified']).astimezone()
file_time = datetime.datetime.fromtimestamp(path.getmtime(dst_file)).astimezone()
if(url_date > file_time):
user_agent = {"User-agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0"}
r = requests.get(url, headers=user_agent)
with open(file, 'wb') as fd:
for chunk in r.iter_content(4096):
fd.write(chunk)