Python程序使用Bing搜索视频

时间:2014-09-12 17:15:55

标签: python video bing-api pyscripter

我一直在尝试使用bing搜索引擎搜索视频。但每次我尝试我都会收到错误HTTPError:HTTPError 403:Forbidden

import urllib
import urllib2
import json

def main():
    query = "'pyscripter'"
    print bing_search(query, 'Video')

def bing_search(query, search_type):
    #search_type: Web, Image, News, Video
    key= 'LsE7jElMmTDfbrnCEmrCmCEBbaPxMG5BvKr9CsfmSNS'
    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()
    json_result = json.loads(response_data)
    result_list = json_result['d']['results']
    print result_list
    return result_list

if __name__ == '__main__':
    main()

显示的错误是:

Traceback (most recent call last):
File "<module1>", line 30, in <module>
File "<module1>", line 7, in main
File "<module1>", line 22, in bing_search
File "C:\Python27\lib\urllib2.py", line 410, in open
    response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 448, in error
    return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 403: Forbidden

在尝试此操作之前,我使用过的YouTube搜索API工作正常。但唯一的问题是它仅限于YouTube数据库中的视频。我想要的是与互联网中存在的关键字相关的所有视频的网址列表。所以我开始使用Bing搜索引擎。任何有关这方面的帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我保存了问题,

Web服务器可以返回403 Forbidden HTTP状态代码,以响应客户端对网页或资源的请求,以指示可以访问服务器并理解该请求,但拒绝采取任何进一步的操作。状态代码403响应是Web服务器被配置为由于某种原因拒绝客户端访问所请求资源的结果。

在我的情况下,我忘了激活&#34; bing search&#34;订阅,请转到&#34; https://datamarket.azure.com/dataset/bing/search&#34;并激活&#34; bing search&#34;订阅