This document discusses adding external search functionality to a Django app by utilizing the Bing Search API. It describes registering for a Bing API key, building a Bing search Python module to run queries and handle the JSON response, and integrating search into the app templates and views. Special considerations for encoding queries as Unicode and handling the response are also covered.
3. Django Summer 3
12.1. The Bing Search API
●
把用 Bing 搜尋到的資料顯示在自己的 APP 裡
●
Bing 伺服器回傳的 result 可以是 XML 或 JSON (可以自己
指定)
4. Django Summer 4
12.1.1. Registering for a Bing API Key
●
註冊 MS 的帳號(可以直接用 hotmail 的)
– https://account.windowsazure.com
●
到 Windows Azure Marketplace Bing Search API page 訂
閱免費版搜尋 API 服務
– 5,000 Transactions/month
7. Django Summer 7
12.2. Adding Search Functionality
●
利用 urllib2 的
password manager
在送出的 HTTP
request header 中加
入我們剛申請好的
API Key 資訊
●
# Setup authentication with the Bing servers.
# The username MUST be a blank string, and put in
your API key!
username = ''
bing_api_key = '<api_key>'
# Create a 'password manager' which handles
authentication for us.
password_mgr =
urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, search_url,
username, bing_api_key)