Код: Выделить всё
def BingSearch(query):
import json
import os
from pprint import pprint
import requests
os.environ['Removed'] = 'https://api.bing.microsoft.com/'
subscription_key = os.environ['Removed']
endpoint = os.environ['Removed'] + "v7.0/search"
mkt = 'en-US'
params = { 'q': query, 'mkt': mkt }
headers = { 'Ocp-Apim-Subscription-Key': subscription_key }
try:
response = requests.get(endpoint, headers=headers, params=params)
response.raise_for_status()
"""
print("Headers: ")
print(response.headers)
print("JSON Response: ")
pprint(response.json())
"""
return response.json()['webPages']['value'] # return the 'value' list directly
except Exception as ex:
print(ex) # print the exception
time.sleep(0.25)
return [] # return an empty list when there's an exception
def print_urls(webPages):
#Cogni_Nexa_2.1.0.clear_screen()
site_Urls_New = []
if webPages: # check if webPages is not empty
for entry in webPages:
site_Urls_New.append(entry['url'])
else:
print("No web pages found.")
return site_Urls_New
def results_All_Querries(querry):
print (querry)
site_urls = []
for q in querry:
webSites = BingSearch(q)
urls = print_urls(webSites)
if urls: # if urls is not None
site_urls.extend(urls) # add the urls to site_urls
#clear_screen()
url_filtered = [url for url in site_urls if url.startswith("https://") or ("HTTPS") or ("http") or ("HTTP") or ("www") or ("WWW")]
clear_screen()
for url in url_filtered:
if url.startswith("https://") or ("http") or ("HTTPS") or ("HTTP") or ("www") or ("WWW"):
print(url+"\n")
else:
print ("\n")
return
Подробнее здесь: https://stackoverflow.com/questions/786 ... wser-cache