<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 12.7.?搜索 Google 讓我們回到這章開始時你看到的那段代碼,獲得比當前氣溫更有價值和令人振奮的信息。 Google 提供了一個 SOAP API,以便通過程序進行 Google 搜索。使用它的前提是,你注冊了 Google 網絡服務。 ## 過程?12.4.?注冊 Google 網絡服務 1. 訪問 [http://www.google.com/apis/](http://www.google.com/apis/) 并創建一個賬號。唯一的需要是提供一個 E-mail 地址。注冊之后,你將通過 E-mail 收到你的 Google API 許可證 (license key)。你需要在調用 Google 搜索函數時使用這個許可證。 2. 還是在 [http://www.google.com/apis/](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') >>> server.methods.keys() [u'doGoogleSearch', u'doGetCachedPage', u'doSpellingSuggestion'] >>> callInfo = server.methods['doGoogleSearch'] >>> for arg in callInfo.inparams: ... 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 提供三個函數:`doGoogleSearch`、`doGetCachedPage` 和 `doSpellingSuggestion`。顧名思義,執行 Google 搜索并返回結果;獲得 Google 最后一次掃描該頁時獲得的緩存;基于常見拼寫錯誤提出單詞拼寫建議。 | | \[3\] | `doGoogleSearch` 函數需要一系列不同類型的參數。注意:WSDL 文件可以告訴你有哪些參數和他們的參數類型,但不能告訴你它們的含義和使用方法。在參數值有限定的情況下,理論上它能夠告訴你參數的取值范圍,但 Google 的 WSDL 沒有那么細化。`WSDL.Proxy` 不會變魔術,它只能給你 WSDL 文件中提供的信息。 | 這里簡要地列出了 `doGoogleSearch` 函數的所有參數: * `key`――你注冊 Google 網絡服務時獲得的 Google API 許可證。 * `q`――你要搜索的詞或詞組。其語法與 Google 的網站表單處完全相同,你所知道的高級搜索語法和技巧這里完全適用。 * `start`――起始的結果編號。與使用 Google 網頁交互搜索時相同,這個函數每次返回 10 個結果。如果你需要查看 “第二” 頁結果則需要將 `start` 設置為 10。 * `maxResults`――返回的結果個數。目前的值是 10,當然如果你只對少數返回結果感興趣或者希望節省網絡帶寬,也可以定義為返回更少的結果。 * `filter`――如果設置為 `True`,Google 將會過濾結果中重復的頁面。 * `restrict`――這里設置 `country` 并跟上一個國家代碼可以限定只返回特定國家的結果。例如:`countryUK` 用于在英國搜索頁面。你也可以設定 `linux`,`mac` 或者 `bsd` 以便搜索 Google 定義的技術站點組,或者設為 `unclesam` 來搜索美國政府站點。 * `safeSearch`――如果設置為 `True`,Google 將會過濾掉色情站點。 * `lr` (“language restrict”,語言限制)――這里設置語言限定值返回特定語言的站點。 * `ie` 和 `oe` (“input encoding”,輸入編碼和 “output encoding”,輸出編碼)――不贊成使用,都應該是 `utf-8`。 ## 例?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") >>> len(results.resultElements) 10 >>> results.resultElements[0].URL '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` 中的每個元素都是一個包含 `URL`、`title`、`snippet` 以及其他屬性的對象。基于這一點,你可以使用諸如 **`dir(results.resultElements[0])`** 的普通 Python 自省技術來查看有效屬性,或者通過 WSDL proxy 對象查看函數的 `outparams`。不同的方法能帶給你相同的結果。 | `results` 對象中所加載的不僅僅是實際的搜索結果。它也含有搜索行為自身的信息,比如耗時和總結果數等 (盡管只返回了10條結果)。Google 網頁界面中顯示了這些信息,通過程序你也同樣能獲得它們。 ## 例?12.14.?從Google獲得次要信息 ``` >>> results.searchTime 0.224919 >>> results.estimatedTotalResultsCount 29800000 >>> results.directoryCategories [<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](http://directory.google.com/) 中的類別列表。你可以用這些 URLs 到 [http://directory.google.com/](http://directory.google.com/) 建立到 directory category 頁面的鏈接。 |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看