Showing posts with label windows azure. Show all posts
Showing posts with label windows azure. Show all posts

Wednesday, September 5, 2012

Use Python to Access (Windows Azure) Bing Search API

As discussed in previous post, Microsoft had updated its Bing Search API and integrate it into the Windows Azure Marketplace.  But how to access the Search API in your web apps? In this doc, there are examples in Visual Basic and PHP. However, what if you like to use Python? There are a few guides on the Internet about Python wrapper on  Bing Search API, but many of them are out-dated (they are for old APIs). For the rest that work, many of them require you to install certain package, which is annoying sometimes. Actually, the python code to access Bing Search API is quite simple and the key is to do authentication.

The following shows an example:
import urllib2

# create credential for authentication
user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; FDM; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 1.1.4322)'
key= 'YOUR ACCOUNT KEY'
creds = (':%s' % key).encode('base64')[:-1]
auth = 'Basic %s' % creds

request = urllib2.Request('https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query=%27test%27&$top=5&$format=json')
request.add_header('Authorization', auth)
request.add_header('User-Agent', user_agent)

requestor = urllib2.build_opener()
result = requestor.open(request) 
print result.read()

Here, 'key' is your Account key in Windows Azure. You can find it here. You can replace the url used in urllib2.Request() to search what you want.

Tuesday, August 28, 2012

Using Bing Search API (使用 Bing API)

*updated: Bing had changed its api without updating the documentation. Now we need to use something like "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=..."

Bing Search API had been moved to Windows Azure Marketplace. The way to use it also slightly changed (e.g., no ApplicationID any more). The perquisites to use Bing Search API is to have a account key in Windows Azure Marketplace. You may need to register in the Marketplace first.

After having the account key, using Bing Search API is straightforward. For example, if we want to search "iphone5", we can do
https://api.datamarket.azure.com/Bing/Search/Web/?Query=%27iphone5%27
 https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=%27iphone5%27
If you want top10 results, do
https://api.datamarket.azure.com/Bing/Search/Web/?Query=%27iphone5%27&$top=10 
https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?
Query=%27iphone5%27&$top=10  
If you further want results in format in JSON, do
https://api.datamarket.azure.com/Bing/Search/Web/?Query=%27iphone5%27&$top=10&$format=JSON
https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web
?Query=%27iphone5%27&$top=10&$format=JSON 
You may wonder where the authentication happens since we no longer have an ApplicationID in the url. When you type in the search url, the browser will automatically pop up a dialog asking for user name and password. Leaving the user name blank and type your account key as password. Then you can see the search results right away.

What if you want to search for images? just do
https://api.datamarket.azure.com/Bing/Search/Image/?Query=%27iphone5%27
https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query=%27iphone5%27 
 This post only introduces some basic use of Bing Search API. For more uses, please look at this link.