chatbot/scraper_functions.py

16 lines
695 B
Python
Raw Normal View History

2024-02-06 01:21:53 +00:00
import requests
from bs4 import BeautifulSoup
from urllib.parse import quote
2024-02-06 01:21:53 +00:00
def query_external_website(base_url, query):
2024-02-06 01:37:19 +00:00
try:
page = requests.get(base_url + quote(query))
2024-02-06 01:37:19 +00:00
soup = BeautifulSoup(page.content, "html.parser")
title = soup.find("span", class_="mw-page-title-main").text
content = next((paragraph for paragraph in soup.find(id="mw-content-text").select("p") if not paragraph.has_attr("class")), None)
2024-02-06 02:37:26 +00:00
if content == None:
raise Exception("Can't parse")
return "\nTITLE:\n" + title + "\n\nCONTENT:\n" + content.text + "\n\nFULL LINK:\n" + base_url + quote(query)
2024-02-06 01:37:19 +00:00
except:
return "Can't parse search result :("