Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python class for Wikipeida seaching
import requests
from bs4 import BeautifulSoup as bs
class Wikipedia():
def getTitles(self,query,num=4):
url="https://en.wikipedia.org/w/api.php?action=query&list=search&srnamespace=0&format=json&srlimit={}&srsearch={}&srprop=".format(num,query)
data = requests.get(url)
if data.json().get("query").get("searchinfo").get("totalhits") > 0:
titles = [ title.get("title") for title in data.json().get("query").get("search")]
else:
titles = False
return titles
def getSummary(self,title):
try:
data = requests.get("https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&titles={}&exintro=1".format(title))
raw_extract = data.json()["query"]["pages"].itervalues().next()["extract"]
soup = bs(raw_extract)
return soup.find('p').text
except:
return False
def process(self,message):
titles = self.getTitles(message)
if titles:
response = self.getSummary(titles[0])
return {"id": titles[0],
"text_response": response,
"recommendations": titles
}
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment