<pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
    <pre id="vvttv"></pre>

      <p id="vvttv"></p>

          <p id="vvttv"></p>

                <p id="vvttv"></p>

                <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                  <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                    <p id="vvttv"></p>

                    11.2. 避免通過 HTTP 重復地獲取數據

                    假如說你想用 HTTP 下載資源,例如一個 Atom feed 匯聚。你不僅僅想下載一次;而是想一次又一次地下載它,如每小時一次,從提供 news feed 的站點獲得最新的消息。讓我們首先用一種直接而原始的方法來實現它,然后看看如何改進它。

                    例 11.2. 用直接而原始的方法下載 feed

                    >>> import urllib
                    >>> data = urllib.urlopen('http://diveintomark.org/xml/atom.xml').read()    1
                    >>> 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" />
                      <-- rest of feed omitted for brevity -->
                    
                    1 使用 Python 通過 HTTP 下載任何東西都簡單得令人難以置信;實際上,只需要一行代碼。urllib 模塊有一個便利的 urlopen 函數,它接受您所要獲取的頁面地址,然后返回一個類文件對象,您僅僅使用 read() 便可獲得頁面的全部內容。這再簡單不過了。

                    那么這種方法有何不妥之處嗎?當然,在測試或開發中一次性的使用沒有什么不妥。我經常這樣。我想要 feed 匯聚的內容,我就獲取 feed 的內容。這種方法對其他 web 頁面同樣有效。但是一旦你開始按照 web 服務的方式去思考有規則的訪問需求時 (記住,你說你計劃每小時一次地重復獲取這樣的 feed ) 就會發現這樣的做法效率實在是太低了,并且對服務器來說也太笨了。

                    下面先談論一些 HTTP 的基本特性。

                      <pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
                      <pre id="vvttv"></pre>

                        <p id="vvttv"></p>

                            <p id="vvttv"></p>

                                  <p id="vvttv"></p>

                                  <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                                    <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                                      <p id="vvttv"></p>

                                      这里只有精品视频