<span id="7ztzv"></span>
<sub id="7ztzv"></sub>

<span id="7ztzv"></span><form id="7ztzv"></form>

<span id="7ztzv"></span>

        <address id="7ztzv"></address>

            12.7. 搜索 Google

            讓我們回到這章開始時你看到的那段代碼,獲得比當前氣溫更有價值和令人振奮的信息。

            Google 提供了一個 SOAP API,以便通過程序進行 Google 搜索。使用它的前提是,你注冊了 Google 網絡服務。

            過程 12.4. 注冊 Google 網絡服務

            1. 訪問 http://www.google.com/apis/ 并創建一個賬號。唯一的需要是提供一個 E-mail 地址。注冊之后,你將通過 E-mail 收到你的 Google API 許可證 (license key)。你需要在調用 Google 搜索函數時使用這個許可證。

            2. 還是在 http://www.google.com/apis/ 上,下載 Google 網絡 APIs 開發工具包 (Google Web APIs developer kit)。它包含著包括 Python 在內的多種語言的樣例代碼,更重要的是它包含著 WSDL 文件。

            3. 解壓這個開發工具包并找到 GoogleSearch.wsdl。將這個文件拷貝到你本地驅動器的一個永久地址。在本章后面位置你會用到它。

            你有了開發許可證和 Google WSDL 文件之后就可以和 Google 網絡服務打交道了。

            例 12.12. 內省 Google 網絡服務

            >>> from SOAPpy import WSDL
            >>> server = WSDL.Proxy('/path/to/your/GoogleSearch.wsdl') 1
            >>> server.methods.keys()                                  2
            [u'doGoogleSearch', u'doGetCachedPage', u'doSpellingSuggestion']
            >>> callInfo = server.methods['doGoogleSearch']
            >>> for arg in callInfo.inparams:                          3
            ...     print arg.name.ljust(15), arg.type
            key             (u'http://www.w3.org/2001/XMLSchema', u'string')
            q               (u'http://www.w3.org/2001/XMLSchema', u'string')
            start           (u'http://www.w3.org/2001/XMLSchema', u'int')
            maxResults      (u'http://www.w3.org/2001/XMLSchema', u'int')
            filter          (u'http://www.w3.org/2001/XMLSchema', u'boolean')
            restrict        (u'http://www.w3.org/2001/XMLSchema', u'string')
            safeSearch      (u'http://www.w3.org/2001/XMLSchema', u'boolean')
            lr              (u'http://www.w3.org/2001/XMLSchema', u'string')
            ie              (u'http://www.w3.org/2001/XMLSchema', u'string')
            oe              (u'http://www.w3.org/2001/XMLSchema', u'string')
            
            1 步入 Google 網絡服務很簡單:建立一個 WSDL.Proxy 對象并指向到你復制到本地的 Google WSDL 文件。
            2 WSDL 文件可知,Google 提供三個函數:doGoogleSearchdoGetCachedPagedoSpellingSuggestion。顧名思義,執行 Google 搜索并返回結果;獲得 Google 最后一次掃描該頁時獲得的緩存;基于常見拼寫錯誤提出單詞拼寫建議。
            3 doGoogleSearch 函數需要一系列不同類型的參數。注意:WSDL 文件可以告訴你有哪些參數和他們的參數類型,但不能告訴你它們的含義和使用方法。在參數值有限定的情況下,理論上它能夠告訴你參數的取值范圍,但 Google 的 WSDL 沒有那么細化。WSDL.Proxy 不會變魔術,它只能給你 WSDL 文件中提供的信息。

            這里簡要地列出了 doGoogleSearch 函數的所有參數:

            例 12.13. 搜索 Google

            >>> from SOAPpy import WSDL
            >>> server = WSDL.Proxy('/path/to/your/GoogleSearch.wsdl')
            >>> key = 'YOUR_GOOGLE_API_KEY'
            >>> results = server.doGoogleSearch(key, 'mark', 0, 10, False, "",
            ...     False, "", "utf-8", "utf-8")             1
            >>> len(results.resultElements)                  2
            10
            >>> results.resultElements[0].URL                3
            'http://diveintomark.org/'
            >>> results.resultElements[0].title
            'dive into <b>mark</b>'
            
            1 在設置好 WSDL.Proxy 對象之后,你可以使用十個參數來調用 server.doGoogleSearch。記住要使用你注冊 Google 網絡服務時授權給你自己的 Google API 許可證。
            2 有很多的返回信息,但我們還是先來看一下實際的返回結果。它們被存儲于 results.resultElements 之中,你可以像使用普通的 Python 列表那樣來調用它。
            3 resultElements 中的每個元素都是一個包含 URLtitlesnippet 以及其他屬性的對象。基于這一點,你可以使用諸如 dir(results.resultElements[0]) 的普通 Python 自省技術來查看有效屬性,或者通過 WSDL proxy 對象查看函數的 outparams。不同的方法能帶給你相同的結果。

            results 對象中所加載的不僅僅是實際的搜索結果。它也含有搜索行為自身的信息,比如耗時和總結果數等 (盡管只返回了10條結果)。Google 網頁界面中顯示了這些信息,通過程序你也同樣能獲得它們。

            例 12.14. 從Google獲得次要信息

            >>> results.searchTime                     1
            0.224919
            >>> results.estimatedTotalResultsCount     2
            29800000
            >>> results.directoryCategories            3
            [<SOAPpy.Types.structType item at 14367400>:
             {'fullViewableName':
              'Top/Arts/Literature/World_Literature/American/19th_Century/Twain,_Mark',
              'specialEncoding': ''}]
            >>> results.directoryCategories[0].fullViewableName
            'Top/Arts/Literature/World_Literature/American/19th_Century/Twain,_Mark'
            
            1 這個搜索耗時 0.224919 秒。這不包括用于發送和接收 SOAP XML 文檔的時間,僅僅是 Google 在接到搜索請求后執行搜索所花費的時間。
            2 總共有接近 30,000,000 個結果信息。通過讓 start 參數以 10 遞增來重復調用 server.doGoogleSearch,你能夠獲得全部的結果。
            3 對于有些請求,Google 還返回一個 Google Directory 中的類別列表。你可以用這些 URLs 到 http://directory.google.com/ 建立到 directory category 頁面的鏈接。
            <span id="7ztzv"></span>
            <sub id="7ztzv"></sub>

            <span id="7ztzv"></span><form id="7ztzv"></form>

            <span id="7ztzv"></span>

                  <address id="7ztzv"></address>

                      亚洲欧美在线