-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_atasozleri.py
67 lines (49 loc) · 1.57 KB
/
get_atasozleri.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# https://tr.wiktionary.org/wiki/Kategori:Türkçe_atasözleri
# https://tr.wiktionary.org/wiki/Kategori:Türkçe_deyimler
# https://tr.wiktionary.org/wiki/%C3%96zel:ApiHelp
# https://mediawiki.org/wiki/API:Categorymembers/tr#Python
# https://mediawiki.org/wiki/API:Parsing_wikitext/tr#Python
import pandas as pd
import requests
import os
S = requests.Session()
URL = "https://tr.wiktionary.org/w/api.php"
# I. GET CATEGORYMEMBERS
# https://tr.wiktionary.org/w/api.php?action=query&list=categorymembers&cmtitle=Kategori:Türkçe_atasözleri&cmlimit=20
PARAMS_1 = {
"action": "query",
"list": "categorymembers",
"cmtitle": "Kategori:Türkçe_atasözleri",
"cmlimit": "max",
"format": "json"
}
pages = []
while True:
R = S.get(url=URL, params=PARAMS_1)
data = R.json()
pages.extend(data['query']['categorymembers'])
try:
PARAMS_1['cmcontinue'] = data['continue']['cmcontinue']
except KeyError:
break
#print(len(pages))
# II. GET PARSED MEMBERS
# https://tr.wiktionary.org/w/api.php?action=parse&pageid=300336&prop=wikitext
PARAMS_2 = {
"action": "parse",
"pageid": "0",
"prop": "wikitext",
"format": "json"
}
all_pages = []
for i,p in enumerate(pages):
PARAMS_2['pageid'] = p['pageid']
R = S.get(url=URL, params=PARAMS_2)
data = R.json()
data['parse']['wikitext'] = data['parse']['wikitext']['*']
all_pages.append(data['parse'])
print(f'{i+1}/{len(pages)}')
print("CSV tablosu yaratılıyor...")
df = pd.DataFrame(all_pages)
df.to_csv(f'atasozleri_ham.csv', index=False)
print("Bitti.")