-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurl_input.py
41 lines (26 loc) · 860 Bytes
/
url_input.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
39
40
import requests
def fetch_save_url(url, file_name):
'''
Takes an url as an input, downloads a page and stores it in the file.
'''
try:
response = requests.get(url, timeout=1)
except Exception as e:
print('Error!', e)
return
if response.status_code == 200:
with open(file_name, 'w') as file:
file.write(response.text)
print('ok!')
else:
print('Error, status code =', response.status_code)
if __name__ == '__main__':
# Test cases
# Successful
fetch_save_url('https://github.com/requests/requests', 'request.html')
# Non URL
fetch_save_url('lll', 'request.html')
# Wrong URL
fetch_save_url('https://github.co/requests/requests', 'request.html')
# Non existing page
fetch_save_url('https://github.com/aj3,o', 'request.html')