您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用Python通过Azure API搜索Bing

5b51 2022/1/14 8:22:42 python 字数 3094 阅读 555 来源 www.jb51.cc/python

如何使用关键词搜索Bing的图像? 我可以使用以下方式搜索Google: import urllib2 import json credentialGoogle = '' # Google credentials from: https://console.developers.google.com/ searchString = 'Xbox%20One' top = 20 offset = 0

概述

我可以使用以下方式搜索Google:

import urllib2
import json

credentialGoogle = '' # Google credentials from: https://console.developers.google.com/
searchString = 'XBox%20One'
top = 20
offset = 0
while offset < top:
    url = 'https://ajax.googleapis.com/ajax/services/search/images?' + \
          'v=1.0&q=%s&start=%d&userip=%s' % (searchString,offset,credentialGoogle)

    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    results = json.load(response)

    # process results

    offset += 4     # since Google Api only returns 4 search results at a time

Bing的等价物是什么?据推测它开始于:

keyBing = ''        # Bing key from: https://datamarket.azure.com/account/keys
credentialBing = '' # same as key?
searchString = '%27XBox+One%27'
top = 20
offset = 0

url = 'https://api.datamarket.azure.com/Bing/Search/Image?' + \
      'Query=%s&$top=%d&$skip=%d&$format=json' % (searchString,top,offset)

但凭证是如何设置的?

keyBing = ''        # get Bing key from: https://datamarket.azure.com/account/keys
credentialBing = 'Basic ' + (':%s' % keyBing).encode('base64')[:-1] # the "-1" is to remove the trailing "\n" which encode adds
searchString = '%27XBox+One%27'
top = 20
offset = 0

url = 'https://api.datamarket.azure.com/Bing/Search/Image?' + \
      'Query=%s&$top=%d&$skip=%d&$format=json' % (searchString,offset)

request = urllib2.Request(url)
request.add_header('Authorization',credentialBing)
requestOpener = urllib2.build_opener()
response = requestOpener.open(request) 

results = json.load(response)

# process results

解决方案归功于:http://www.guguncube.com/2771/python-using-the-bing-search-api

总结

以上是编程之家为你收集整理的使用Python通过Azure API搜索Bing全部内容,希望文章能够帮你解决使用Python通过Azure API搜索Bing所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶