bing的搜索api简介

              现在提供api的搜索不多了,主要就google的和bing的。

  Google的现在叫CSE(Custom Search Engine), https://www.google.com/cse/all
分为一般版本和企业版本,企业版本的是要钱的,没广告。一般的版本表明是free,但是。。。。。。。。。。。,一天只能检索100次免费的,后续的话,就要收钱了,1000个query要5刀,疯了@#¥#@¥@#¥

       bing的搜索api稍微厚道点,一个月5000免费的,可以多申请几个就行了,后续相对也比较便宜,具体见https://datamarket.azure.com/dataset/bing/search,后续150刀可以买10万qv,而且阶梯价格,比bing显然划算很多。。。。。。

         bing提供的服务最简单的就是一个http服务,看了下,现在很多api根本用不了,其实用python写也非常简单,一个简单的例子如下所述:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
query_type = ["Web",
        "Videos"]

def main():
    query = "中国好声音"
    result = bing_search(query, 'Web')
    for item in result:
        print "___________________________________________________"
        print item["Title"]
        print item["Description"]
        print item["Url"]
        print item['DisplayUrl']
    #print bing_search(query, 'Image')
 
def bing_search(query, search_type):
    #search_type: Web, Image, News, Video
    key= '******************' #api key
    query = urllib.quote(query)
    # 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)'
    credentials = (':%s' % key).encode('base64')[:-1]
    auth = 'Basic %s' % credentials
    url = 'https://api.datamarket.azure.com/Data.ashx/Bing/Search/'+search_type+'?Query=%27'+query+
'%27&$top=5&$format=json'
    request = urllib2.Request(url)
    request.add_header('Authorization', auth)
    request.add_header('User-Agent', user_agent)
    request_opener = urllib2.build_opener()
    response = request_opener.open(request) 
    response_data = response.read()
    #print response_data
    json_result = json.loads(response_data)
    result_list = json_result['d']['results']
    #print result_list
    return result_list
 
if __name__ == "__main__":
    main()


猜你喜欢

转载自blog.csdn.net/wangliang_f/article/details/23286047