Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	File size: 910 Bytes
			
			| e539b70 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import requests
from string import Template
from bs4 import BeautifulSoup
from dotenv import load_dotenv
from os import getenv
load_dotenv()
google_key = getenv('GOOGLE_KEY')
google_engine = getenv('GOOGLE_ENGINE')
url = Template(f'https://www.googleapis.com/customsearch/v1?key={google_key}&cx={google_engine}&q=$query')
def search_web(query: str) -> list:
    query = '+'.join(query.split())
    results = requests.get(url.substitute(query=query)).json()['items']
    links = [item['link'] for item in results]
    texts = []
    for link in links:
        resp = requests.get(link)
        soup = BeautifulSoup(resp.text, 'html.parser')
        text = []
        # remove lists
        for tag in soup.find_all('li'):
            tag.extract()
        
        tags = soup.find_all('p')
        for tag in tags:
            text.append(tag.text)
        texts.append('\n'.join(text))
    return texts | 
