Hallo Stackoverflow GemeinschaftBing Congitive Web Search API mit Python 3
Ich bin versucht, die neue Bing Cognitive Search API über ein Python-Skript 3 zuzugreifen. Ich konnte Bedrohungen finden auf mit Bing Search 2.0 (seit abgeschrieben), konnte aber nicht ein Beispiel für die neue API identifizieren, die mit Python 3. Ich den folgenden Code verwendet:
import urllib.parse
import urllib.request
import json
import base64
def bing_search(query):
key = 'mysubscription_key'
query = urllib.parse.quote(query)
#Create credentials 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)'
encoded = base64.b64encode(bytes(':%s' % key, 'utf-8'))
credentials = encoded[:-1] # the "-1" is to remove the trailing "\n" which encode adds
print(credentials)
auth = 'Basic %s' % credentials
print(auth)
url = 'https://api.cognitive.microsoft.com/bing/v5.0/search?q=' + query + '&mkt=en-us'
print(url)
#Create the API request
urlrequest = urllib.request.urlopen(url) # in Python3 urllib.request(...) becomes urllib.request.open(...)
urlrequest.add_header('Authentication', auth)
urlrequest.add_header('User Agent', user_agent)
request_opener = urllib.request.build_opener()
# Handle the response
response = request_opener.open(urlrequest)
results = json.load(response)
result_list = results['webPages']['values']
print(result_list)
bing_search('good news')
Leider erhalte ich die nach 'Zugriff verweigert' Fehler. Kann mir jemand in die richtige Richtung zeigen?
C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\python.exe C:/Users/Admin/PycharmProjects/momely/placementarchitect/bingtest.py
b'OmZhNTlmNTBlMmFmMjQyZjhhYmE5MTZlNmZkYThhMDM'
Basic b'OmZhNTlmNTBlMmFmMjQyZjhhYmE5MTZlNmZkYThhMDM'
https://api.cognitive.microsoft.com/bing/v5.0/search?q=good%20news&mkt=en-us
Traceback (most recent call last):
File "C:/Users/Admin/PycharmProjects/momely/placementarchitect/bingtest.py", line 34, in <module>
bing_search('good news')
File "C:/Users/Admin/PycharmProjects/momely/placementarchitect/bingtest.py", line 22, in bing_search
urlrequest = urllib.request.urlopen(url) # in Python3 urllib.request(...) becomes urllib.request.open(...)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 162, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 471, in open
response = meth(req, response)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 581, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 509, in error
return self._call_chain(*args)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 443, in _call_chain
result = func(*args)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 589, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Access Denied
Danke nett M.
Danke woozyking. Leider erhalte ich immer noch den gleichen Fehler: urllib.error.HTTPError: HTTP-Fehler 401: Zugriff verweigert – Z101
Was haben Sie genau geändert? – woozyking
Vielen Dank die Anfragen Alternative funktionierte gut. Sehr geschätzt. – Z101