0
# Page Navigation
1
2
Access Wikipedia's link structure including internal page links, backlinks, and language translations. This functionality enables navigation between related pages and discovery of page relationships across Wikipedia's interconnected content.
3
4
## Capabilities
5
6
### Internal Links
7
8
Access pages that are linked from the current page through internal Wikipedia links.
9
10
```python { .api }
11
class WikipediaPage:
12
@property
13
def links(self) -> dict[str, WikipediaPage]:
14
"""
15
Get all internal links from this page to other Wikipedia pages.
16
17
Returns:
18
Dictionary mapping page titles to WikipediaPage objects.
19
Keys are page titles, values are WikipediaPage instances.
20
"""
21
```
22
23
#### Usage Examples
24
25
```python
26
import wikipediaapi
27
28
wiki = wikipediaapi.Wikipedia('MyApp/1.0', 'en')
29
page = wiki.page('Artificial_intelligence')
30
31
# Get all internal links
32
links = page.links
33
print(f"Page has {len(links)} internal links")
34
35
# Browse some links
36
for i, (title, linked_page) in enumerate(links.items()):
37
if i >= 5: # Show first 5 links
38
break
39
print(f"Link: {title}")
40
print(f" Language: {linked_page.language}")
41
print(f" Namespace: {linked_page.namespace}")
42
43
# Access specific linked page
44
if 'Machine learning' in links:
45
ml_page = links['Machine learning']
46
print(f"Found link to: {ml_page.title}")
47
if ml_page.exists():
48
print(f"Summary: {ml_page.summary[:100]}...")
49
50
# Navigate through links
51
python_page = wiki.page('Python_(programming_language)')
52
for link_title, link_page in python_page.links.items():
53
if 'artificial' in link_title.lower():
54
print(f"AI-related link: {link_title}")
55
# Could continue navigating from link_page.links
56
```
57
58
### Backlinks
59
60
Find pages that link to the current page, enabling reverse navigation through Wikipedia's link structure.
61
62
```python { .api }
63
class WikipediaPage:
64
@property
65
def backlinks(self) -> dict[str, WikipediaPage]:
66
"""
67
Get pages that link to this page.
68
69
Returns:
70
Dictionary mapping page titles to WikipediaPage objects.
71
Keys are titles of pages that link here, values are WikipediaPage instances.
72
"""
73
```
74
75
#### Usage Examples
76
77
```python
78
page = wiki.page('Neural_network')
79
80
# Get pages that link to this page
81
backlinks = page.backlinks
82
print(f"Page has {len(backlinks)} backlinks")
83
84
# Explore pages that reference this topic
85
for title, referring_page in backlinks.items():
86
print(f"Referenced by: {title}")
87
if referring_page.namespace == wikipediaapi.Namespace.MAIN:
88
print(f" Main article: {title}")
89
elif referring_page.namespace == wikipediaapi.Namespace.CATEGORY:
90
print(f" Category: {title}")
91
92
# Find related research areas
93
ml_page = wiki.page('Machine_learning')
94
backlinks = ml_page.backlinks
95
96
# Look for academic or research-related pages
97
research_pages = []
98
for title, page in backlinks.items():
99
if any(keyword in title.lower() for keyword in ['research', 'university', 'algorithm']):
100
research_pages.append(title)
101
102
print(f"Found {len(research_pages)} research-related backlinks")
103
for title in research_pages[:10]: # Show first 10
104
print(f" - {title}")
105
```
106
107
### Language Links
108
109
Access translations and equivalent pages in other language editions of Wikipedia.
110
111
```python { .api }
112
class WikipediaPage:
113
@property
114
def langlinks(self) -> dict[str, WikipediaPage]:
115
"""
116
Get language versions of this page.
117
118
Returns:
119
Dictionary mapping language codes to WikipediaPage objects.
120
Keys are language codes (e.g., 'es', 'fr', 'de'),
121
values are WikipediaPage instances for those languages.
122
"""
123
```
124
125
#### Usage Examples
126
127
```python
128
page = wiki.page('Climate_change')
129
130
# Get all language versions
131
lang_versions = page.langlinks
132
print(f"Page available in {len(lang_versions)} languages")
133
134
# Browse available languages
135
for lang_code, lang_page in lang_versions.items():
136
print(f"{lang_code}: {lang_page.title}")
137
138
# Access specific language versions
139
if 'es' in lang_versions:
140
spanish_page = lang_versions['es']
141
print(f"Spanish title: {spanish_page.title}")
142
print(f"Spanish URL: {spanish_page.fullurl}")
143
print(f"Spanish summary: {spanish_page.summary[:100]}...")
144
145
if 'fr' in lang_versions:
146
french_page = lang_versions['fr']
147
print(f"French title: {french_page.title}")
148
149
# Work with multiple languages
150
page_en = wiki.page('Quantum_computing')
151
if page_en.exists():
152
langs = page_en.langlinks
153
154
# Create Wikipedia instances for other languages
155
wiki_de = wikipediaapi.Wikipedia('MyApp/1.0', 'de')
156
wiki_ja = wikipediaapi.Wikipedia('MyApp/1.0', 'ja')
157
158
if 'de' in langs:
159
german_title = langs['de'].title
160
german_page_direct = wiki_de.page(german_title)
161
print(f"German page: {german_page_direct.title}")
162
163
if 'ja' in langs:
164
japanese_title = langs['ja'].title
165
japanese_page_direct = wiki_ja.page(japanese_title)
166
print(f"Japanese page: {japanese_page_direct.title}")
167
```
168
169
### Cross-Language Navigation
170
171
Navigate between different language editions and compare content across languages.
172
173
#### Usage Examples
174
175
```python
176
def compare_languages(topic, languages):
177
"""Compare a topic across multiple languages."""
178
results = {}
179
180
for lang in languages:
181
wiki_lang = wikipediaapi.Wikipedia('MyApp/1.0', lang)
182
183
# Try to find the page in this language
184
page = wiki_lang.page(topic)
185
if page.exists():
186
results[lang] = {
187
'title': page.title,
188
'summary_length': len(page.summary),
189
'sections': len(page.sections),
190
'url': page.fullurl
191
}
192
else:
193
# Try to find via English page's language links
194
wiki_en = wikipediaapi.Wikipedia('MyApp/1.0', 'en')
195
en_page = wiki_en.page(topic)
196
if en_page.exists() and lang in en_page.langlinks:
197
lang_page = en_page.langlinks[lang]
198
results[lang] = {
199
'title': lang_page.title,
200
'summary_length': len(lang_page.summary),
201
'sections': len(lang_page.sections),
202
'url': lang_page.fullurl
203
}
204
205
return results
206
207
# Compare "Artificial Intelligence" across languages
208
comparison = compare_languages('Artificial_intelligence', ['en', 'es', 'fr', 'de', 'zh'])
209
for lang, info in comparison.items():
210
print(f"{lang}: {info['title']} ({info['summary_length']} chars, {info['sections']} sections)")
211
```
212
213
### Link Analysis and Navigation Patterns
214
215
Advanced patterns for analyzing and navigating Wikipedia's link structure.
216
217
#### Usage Examples
218
219
```python
220
def find_common_links(page1_title, page2_title):
221
"""Find pages that both pages link to."""
222
wiki = wikipediaapi.Wikipedia('MyApp/1.0', 'en')
223
224
page1 = wiki.page(page1_title)
225
page2 = wiki.page(page2_title)
226
227
if not (page1.exists() and page2.exists()):
228
return []
229
230
links1 = set(page1.links.keys())
231
links2 = set(page2.links.keys())
232
233
common_links = links1.intersection(links2)
234
return sorted(common_links)
235
236
def find_linking_path(start_page_title, target_page_title, max_depth=2):
237
"""Find if there's a linking path between two pages."""
238
wiki = wikipediaapi.Wikipedia('MyApp/1.0', 'en')
239
240
start_page = wiki.page(start_page_title)
241
if not start_page.exists():
242
return None
243
244
# Check direct link
245
if target_page_title in start_page.links:
246
return [start_page_title, target_page_title]
247
248
# Check two-hop links
249
if max_depth >= 2:
250
for intermediate_title, intermediate_page in start_page.links.items():
251
if intermediate_page.exists():
252
intermediate_links = intermediate_page.links
253
if target_page_title in intermediate_links:
254
return [start_page_title, intermediate_title, target_page_title]
255
256
return None
257
258
# Example usage
259
common = find_common_links('Machine_learning', 'Artificial_intelligence')
260
print(f"Common links: {len(common)}")
261
for link in common[:10]:
262
print(f" - {link}")
263
264
path = find_linking_path('Python_(programming_language)', 'Machine_learning')
265
if path:
266
print(f"Path found: {' -> '.join(path)}")
267
else:
268
print("No direct path found")
269
```