-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwikisearcher.py
More file actions
36 lines (29 loc) · 947 Bytes
/
wikisearcher.py
File metadata and controls
36 lines (29 loc) · 947 Bytes
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
import sys
import requests
def get_wikipedia_url(title):
# Encode spaces as underscores (Wikipedia's URL format)
formatted_title = title.strip().replace(' ', '_')
# Use the Wikipedia API to check if the page exists
url = "https://en.wikipedia.org/w/api.php"
params = {
"action": "query",
"titles": formatted_title,
"format": "json"
}
response = requests.get(url, params=params)
data = response.json()
pages = data.get("query", {}).get("pages", {})
page_id = next(iter(pages))
if page_id != "-1":
return f"https://en.wikipedia.org/wiki/{formatted_title}"
else:
return None
if __name__ == "__main__":
file = sys.argv[1]
with open(file) as f:
for title in f:
title = title.strip()
title = ' '.join(title.rsplit(' ', 1)[0:1])
url = get_wikipedia_url(title)
if url:
print(url)