您当前位置:首页 > 深入 Python > HTTP Web 服务 > 如何不通过 HTTP 获取数据 | << >> | ||||
深入 Python从 Python 新手到专家 |
假设您想通过 HTTP 下载资源,例如联合 Atom 提要。但您不只是想下载一次;您想每小时下载一次,以获取提供新闻提要的网站的最新消息。让我们先用快速而粗糙的方式来做,然后再看看如何做得更好。
>>> import urllib >>> data = urllib.urlopen('http://diveintomark.org/xml/atom.xml').read()>>> print data <?xml version="1.0" encoding="iso-8859-1"?> <feed version="0.3" xmlns="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="en"> <title mode="escaped">dive into mark</title> <link rel="alternate" type="text/html" href="http://diveintomark.org/"/> <-- rest of feed omitted for brevity -->
那么这有什么问题呢?嗯,对于测试或开发过程中的一次性快速操作来说,这没有任何问题。我一直这样做。我想要提要的内容,我得到了提要的内容。同样的技术适用于任何网页。但是,一旦您开始考虑要定期访问的 Web 服务(请记住,您说过您计划每小时检索一次此联合提要),那么您就是在效率低下,而且您很不礼貌。
让我们谈谈 HTTP 的一些基本特性。
<< HTTP Web 服务 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
HTTP 的特性 >> |