[PYTHON] How to use bing search api
Constraint
- Free up to 5000 requests per month
- 10000 request $ 20, it transition pay-as-you-go
- https://datamarket.azure.com/dataset/5BA839F1-12CE-4CCE-BF57-A49D98D29A44
How to use
- Create a Microsoft account and get an API KEY
- API KEY is used for basic authentication to endpoint
- API KEY for both user name and password
- Some parameters contain $ and cannot be accessed if encoded
- For some reason, I got a 400 bad request when accessing using urllib.
- Similar phenomenon can be confirmed by stack overflow etc., and it is recommended to use the requests module as a solution. I'm not sure why
- Queries are not recognized unless enclosed in single quotes
- Note that $ format disappears from the URL that goes into the returned __next
- The number of searches cannot be obtained
- It seems that skip can only be specified up to 1000. Since 50 is the maximum for top, you can only get up to 1050
Sample code
bingsearch.py
#! /usr/bin/python
# -*- coding:utf-8 -*-
import urllib
import requests
import json
NUM = 100
key = '' #Enter api key
url = 'https://api.datamarket.azure.com/Bing/Search/Web?'
json_param = '&$format=json'
param = {
'Query':"''", #Put a query
}
req_url = url + urllib.urlencode(param)
for i in range(0,NUM):
r = requests.get(req_url + json_param,auth=(key,key)).json
print json.dumps(r)
req_url = r['d']['__next']