-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikipedia.py
38 lines (31 loc) · 1.05 KB
/
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
import wikipediaapi
import csv
wiki_wiki = wikipediaapi.Wikipedia('MyProjectName ([email protected])', 'en')
def find_links(page):
titles = []
page_py = wiki_wiki.page(page)
links = page_py.links
for title in sorted(links.keys()):
titles.append(title)
return titles
# Collect all rows to be written to the CSV at once
rows = [["id1","id2"]]
original_pages = ['Discrete mathematics', 'Portland, Oregon', 'Mars', 'Pizza', 'Honeybees', 'Great Wall of China']
pages = []
for page in original_pages:
pages.append(page)
for link in original_pages:
pages = pages + find_links(link)
pages = list(set(pages))
print("len: " + str(len(pages)))
for i, page in enumerate(pages):
if not i%((int)(len(pages)/100)):
print(i)
links = find_links(page)
links = [link for link in links if link in pages]
for link in links:
rows.append([page, link])
# Write all rows to the CSV at once
with open('output.csv', mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerows(rows)