-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwikipedia.py
More file actions
executable file
·58 lines (42 loc) · 1.1 KB
/
wikipedia.py
File metadata and controls
executable file
·58 lines (42 loc) · 1.1 KB
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
#!/usr/bin/python3
import urllib.request
from bs4 import BeautifulSoup
import sys
import re
import time
import urllib
usage = '''Usage:
python wikipedia.py <command> [content id]
'''
description = '''Description:
Available commands are page and help.
'''
examples = '''Examples:
python wikipedia.py page "파이썬"
'''
def pagecontent(url):
result = ''
with urllib.request.urlopen(url) as f:
html = f.read().decode('utf-8')
soup = BeautifulSoup(html, 'html.parser')
for content in soup.select('.mw-body'):
result += content.text
return result
if not 2 <= len(sys.argv) <= 3:
print("Number of arguments is not matched")
print("Try \"python wikipedia.py help\".")
sys.exit(1)
cmd = sys.argv[1]
if cmd == "help":
print(usage)
print(description)
print(examples)
sys.exit()
contentid = sys.argv[2]
if cmd == "page":
pageurl = 'https://ko.wikipedia.org/wiki/' + urllib.parse.quote(contentid)
print(pagecontent(pageurl))
else:
print(f"Not available command: {cmd}")
print("Try \"python wikipedia.py help\".")
sys.exit(usage)