am
commited on
Commit
·
bec87fe
1
Parent(s):
70ee9cc
Added wikipedia_api.py
Browse filesFormer-commit-id: e084890b14d00a6a6a97dfa6273d82ab3d69d63c
Former-commit-id: 8ba57d5f5618997ecac2871bd93c1babf1a99ee6
- wikipedia_api.py +37 -0
wikipedia_api.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import wikipedia
|
2 |
+
from bs4 import BeautifulSoup as bs
|
3 |
+
|
4 |
+
wikipedia.set_lang("ru")
|
5 |
+
|
6 |
+
#Принимает список поиска
|
7 |
+
#Возвращает список словарей {'find':строка поиска,'summary': краткое содержание,
|
8 |
+
# 'latitude': широта, 'longitude': долгота, '':}
|
9 |
+
def getWikipedia(pList):
|
10 |
+
|
11 |
+
result = []
|
12 |
+
for i in pList:
|
13 |
+
|
14 |
+
latitude, longitude = '', ''
|
15 |
+
|
16 |
+
get = wikipedia.search(i)
|
17 |
+
if not get is None:
|
18 |
+
|
19 |
+
summary = wikipedia.summary(get[0])
|
20 |
+
|
21 |
+
page = wikipedia.page(get[0])
|
22 |
+
soup = bs(page.html(), 'html.parser')
|
23 |
+
find = soup.find("a", {"class": "mw-kartographer-maplink"})
|
24 |
+
if not find is None:
|
25 |
+
latitude = find['data-lat']
|
26 |
+
longitude = find['data-lon']
|
27 |
+
else:
|
28 |
+
summary = 'Ничего не найдено'
|
29 |
+
|
30 |
+
res = {'find':i,
|
31 |
+
'summary':summary,
|
32 |
+
'latitude':latitude,
|
33 |
+
'longitude':longitude,
|
34 |
+
}
|
35 |
+
result.append(res)
|
36 |
+
|
37 |
+
return result
|