-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki.py
39 lines (30 loc) · 1002 Bytes
/
wiki.py
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
35
36
37
38
39
import wikipedia
import json
def find_between(s, first, last):
"""
Find string between 2 substrings
:param s: content, original string
:param first: first substring
:param last: second substring
:return: found substring or empty string if nothing was found
"""
try:
start = s.index(first) + len(first)
end = s.index(last, start)
return s[start:end]
except ValueError:
return ""
def search_articles(key_phrase):
return wikipedia.search(key_phrase)
def get_page(article):
if len(article.strip()) == 0:
return None
try:
return wikipedia.page(article)
except wikipedia.exceptions.PageError:
return None
except wikipedia.exceptions.WikipediaException:
return None
def get_see_also_links(content):
# wikipedia articles that could be also parsed to get information about the category
return find_between(content, "== See also ==", "== References ==").strip().splitlines()