如何使用 python3 urllib 获取上次修改时间? [英] How can I get the last-modified time with python3 urllib?

查看:27
本文介绍了如何使用 python3 urllib 获取上次修改时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在移植一个我的程序 从 python2 到 python3,我遇到以下错误:AttributeError: 'HTTPMessage' object has no attribute 'getdate'

I'm porting over a program of mine from python2 to python3, and I'm hitting the following error: AttributeError: 'HTTPMessage' object has no attribute 'getdate'

代码如下:

conn = urllib.request.urlopen(fileslist, timeout=30)
last_modified = conn.info().getdate('last-modified')

本节在 python 2.7 下运行,到目前为止我还没有找到在 python 3.1 中获取此信息的正确方法.

This section worked under python 2.7, and so far I haven't been able to find out the correct method to get this information in python 3.1.

完整的上下文是一种更新方法.它将新文件从服务器下拉到其本地数据库,但前提是服务器上的文件比本地文件新.如果有比仅比较本地和远程文件时间戳更聪明的方法来实现此功能,那么我也愿意这样做.

The full context is an update method. It pulls new files from a server down to its local database, but only if the file on the server is newer than the local file. If there's a smarter way to achieve this functionality than just comparing local and remote file timestamps, then I'm open to that as well.

推荐答案

conn.headers['last-modified'] 适用于 Python 2 和 Python 3.比较文件戳对我来说似乎是合理的.

conn.headers['last-modified'] works under both Python 2 and Python 3. Comparing filestamps seems reasonable to me.

这篇关于如何使用 python3 urllib 获取上次修改时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆