-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathweb_utility.py
More file actions
31 lines (25 loc) · 761 Bytes
/
Copy pathweb_utility.py
File metadata and controls
31 lines (25 loc) · 761 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
import sys
# def load_page(url: str) -> str:
def load_page(url):
""" Returns the content of the web page for a valid url.
Otherwise it returns the empty string.
"""
if sys.version_info.major == 3:
from urllib.request import urlopen
from urllib.error import URLError
try:
response = urlopen(url)
if response.status == 200:
body_text = str(response.read())
return body_text
return ""
except URLError:
return ""
else:
from urllib2 import urlopen
from urllib2 import URLError
try:
response = urlopen(url)
return response.read()
except URLError:
return ""