-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_wikipedia.py
53 lines (46 loc) · 1.52 KB
/
utils_wikipedia.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import requests
from bs4 import BeautifulSoup
import wikipedia
def get_wikidata_link(wikipedia_title):
#target_page = wikipedia.page(wikipedia_title)
target_candidates = wikipedia.search(wikipedia_title, results = 5)
for cand in target_candidates:
cand_title = cand.title
if wikipedia_title == cand_title:
target_page = cand
break
target_url = target_page.url
r=requests.get(target_url)
soup = BeautifulSoup(r.text)
#print(soup)
wd_links = soup.findAll("li")
#"Wikidata item" in soup.body
for wd_link in wd_links:
wd_url = ''
if 'Wikidata item' in wd_link.text:
a_element = wd_link.find("a")
wd_url = a_element.get("href")
break
elif 'Wikidata-item' in wd_link.text:
a_element = wd_link.find("a")
wd_url = a_element.get("href")
break
return wd_url
def get_wikipedia_title(target_q, lang):
languages = {lang}
wdt_ids = [target_q]
ids_filter='|'.join(wdt_ids)
languages_filter='|'.join(list(map(lambda x: x + 'wiki', languages)))
#print(languages_filter)
params={
'action': 'wbgetentities',
'props': 'sitelinks',
'ids': ids_filter,
'sitefilter': languages_filter,
'format': 'json'
}
url='https://www.wikidata.org/w/api.php?'
r=requests.get(url, params=params)
j=r.json()
title = j['entities'][target_q]['sitelinks'][f'{lang}wiki']['title']
return title