【发布时间】:2014-09-12 17:15:54
【问题描述】:
我一直在尝试使用必应搜索引擎搜索视频。但是每次我尝试都会收到错误 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 数据库中的视频。我想要的是与互联网中存在的关键字相关的所有视频的 URL 列表。所以我开始使用 Bing 搜索引擎。对此的任何帮助将不胜感激。
【问题讨论】:
-
我运行了这段代码却得到了这个错误:
urllib2.HTTPError: HTTP Error 401: The authorization type you provided is not supported. Only Basic and OAuth are supported -
我建议使用谷歌浏览器,以 cURL 命令的形式获取网络流量,然后尝试一下。那么,使用 Python Request 会让你的生活更轻松。
-
@Lovato 我不知道如何使用 cURL。这是this program 的一个分支
-
@nofinator 我在这方面使用的是 Basic 吗?错误依旧。
-
你不需要知道 curl。只需从 Chrome 复制 cURL 命令,然后将其粘贴到您的终端,看看它是否有效。它必须工作。然后,您将所有标头、有效负载和内容模仿到(如我所建议的)请求模块。我上周自己做的,在这里帮助其他用户。
标签: python video bing-api pyscripter