forked from Decentrala/chatbot
16 lines
677 B
Python
16 lines
677 B
Python
import requests
|
|
from bs4 import BeautifulSoup
|
|
from urllib.parse import quote
|
|
|
|
def query_external_website(base_url, query):
|
|
try:
|
|
page = requests.get(base_url + quote(query))
|
|
soup = BeautifulSoup(page.content, "html.parser")
|
|
title = soup.find(id="firstHeading").text
|
|
content = next((paragraph for paragraph in soup.find(id="mw-content-text").select("p") if not paragraph.has_attr("class")), None)
|
|
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)
|
|
except:
|
|
return "Can't parse search result :("
|