My code
Data mining project simple code
import requests
from bs4 import BeautifulSoup
def get_answer(question):
# Send a GET request to a search engine to get a list of relevant pages
query = question.replace(" ", "+")
url = f"https://www.google.com/search?q={query}"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36"
}
response = requests.get(url, headers=headers)
# Parse the HTML response to extract the first answer
soup = BeautifulSoup(response.text, "html.parser")
answer = soup.find("div", {"class": "BNeawe iBp4i AP7Wnd"}).text
return answer
# Example usage
question = "What is the capital of France?"
answer = get_answer(question)
print(f"The answer is: {answer}")
Comments
Post a Comment