-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathexample.py
executable file
·156 lines (116 loc) · 4.7 KB
/
example.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3
import logging
import wikipediaapi
logging.basicConfig(level=logging.INFO)
user_agent = "Wikipedia-API Example ([email protected])"
wiki_wiki = wikipediaapi.Wikipedia(user_agent=user_agent, language="en")
page_py = wiki_wiki.page("Python_(programming_language)")
print("Page - Exists: %s" % page_py.exists())
print("Page - Id: %s" % page_py.pageid)
print("Page - Title: %s" % page_py.title)
print("Page - Summary: %s" % page_py.summary[0:60])
def print_sections(sections, level=0):
for s in sections:
print("{}: {} - {}".format("*" * (level + 1), s.title, s.text[0:40]))
print_sections(s.sections, level + 1)
print("Sections:")
print_sections(page_py.sections)
def print_langlinks(page):
langlinks = page.langlinks
for k in sorted(langlinks.keys()):
v = langlinks[k]
print(f"{k}: {v.language} - {v.title}: {v.fullurl}")
print("Lang links:")
print_langlinks(page_py)
def print_links(page):
links = page.links
for title in sorted(links.keys()):
print(f"{title}: {links[title]}")
print("Links:")
print_links(page_py)
def print_categories(page):
categories = page.categories
for title in sorted(categories.keys()):
print(f"{title}: {categories[title]}")
print("Categories")
print_categories(page_py)
section_py = page_py.section_by_title("Features and philosophy")
if section_py is not None:
print("Section - Title: %s" % section_py.title)
print("Section - Text: %s" % section_py.text[0:60])
else:
print("Section does not exist.")
wiki_html = wikipediaapi.Wikipedia(
user_agent=user_agent, language="cs", extract_format=wikipediaapi.ExtractFormat.HTML
)
page_ostrava = wiki_html.page("Ostrava")
print("Page - Exists: %s" % page_ostrava.exists())
print("Page - Id: %s" % page_ostrava.pageid)
print("Page - Title: %s" % page_ostrava.title)
print("Page - Summary: %s" % page_ostrava.summary[0:60])
print_sections(page_ostrava.sections)
section_ostrava = page_ostrava.section_by_title("Heraldický znak")
if section_ostrava is not None:
print("Section - Title: %s" % section_ostrava.title)
print("Section - Text: %s" % section_ostrava.text[0:60])
else:
print("Section does not exists")
page_nonexisting = wiki_wiki.page("Wikipedia-API-FooBar")
print("Page - Exists: %s" % page_nonexisting.exists())
print("Page - Id: %s" % page_nonexisting.pageid)
print("Page - Title: %s" % page_nonexisting.title)
print("Page - Summary: %s" % page_nonexisting.summary[0:60])
wiki_de = wikipediaapi.Wikipedia(user_agent=user_agent, language="de")
de_page = wiki_de.page("Deutsche Sprache")
print(de_page.title + ": " + de_page.fullurl)
print(de_page.summary[0:60])
en_page = de_page.langlinks["en"]
print(en_page.title + ": " + en_page.fullurl)
print(en_page.summary[0:60])
def print_categorymembers(categorymembers, level=0, max_level=2):
for c in categorymembers.values():
print("%s %s (ns: %d)" % ("*" * (level + 1), c.title, c.ns))
if c.ns == wikipediaapi.Namespace.CATEGORY and level < max_level:
print_categorymembers(c.categorymembers, level + 1, max_level=max_level)
cat = wiki_wiki.page("Category:Physics")
print("Category members: Category:Physics")
print_categorymembers(cat.categorymembers, max_level=1)
wiki_hi = wikipediaapi.Wikipedia(user_agent=user_agent, language="hi")
# fetch page about Python in Hindu
# https://hi.wikipedia.org/wiki/%E0%A4%AA%E0%A4%BE%E0%A4%87%E0%A4%A5%E0%A4%A8
p_hi_python_quoted = wiki_hi.article(
title="%E0%A4%AA%E0%A4%BE%E0%A4%87%E0%A4%A5%E0%A4%A8",
unquote=True,
)
print(p_hi_python_quoted.title)
print(p_hi_python_quoted.summary[0:60])
# Fetch page about Python in Chinese
wiki_zh = wikipediaapi.Wikipedia(user_agent=user_agent, language="zh")
zh_page = wiki_zh.page("Python")
print(zh_page.title + ": " + zh_page.fullurl)
print(zh_page.summary[0:60])
print(repr(zh_page.varianttitles))
# https://zh.wikipedia.org/zh-cn/Python
wiki_zh_cn = wikipediaapi.Wikipedia(
user_agent=user_agent, language="zh", variant="zh-cn"
)
zh_page_cn = wiki_zh_cn.page("Python")
print(zh_page_cn.title + ": " + zh_page_cn.fullurl)
print(zh_page_cn.summary[0:60])
print(repr(zh_page_cn.varianttitles))
# https://zh.wikipedia.org/zh-tw/Python
wiki_zh_tw = wikipediaapi.Wikipedia(
user_agent=user_agent, language="zh", variant="zh-tw"
)
zh_page_tw = wiki_zh_tw.page("Python")
print(zh_page_tw.title + ": " + zh_page_tw.fullurl)
print(zh_page_tw.summary[0:60])
print(repr(zh_page_tw.varianttitles))
# https://zh.wikipedia.org/zh-sg/Python
wiki_zh_sg = wikipediaapi.Wikipedia(
user_agent=user_agent, language="zh", variant="zh-sg"
)
zh_page_sg = wiki_zh_sg.page("Python")
print(zh_page_sg.title + ": " + zh_page_sg.fullurl)
print(zh_page_sg.summary[0:60])
print(repr(zh_page_sg.varianttitles))