-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathexercise13_2.py
executable file
·36 lines (27 loc) · 922 Bytes
/
exercise13_2.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
#!/usr/bin/env python3
"""
Exercise 13.2: Write a program that will prompt to enter in a url, read the JSON data from that URL using
urllib and then parse and extract the comment counts from the JSON data,
compute the sum of the numbers in the file and enter the sum below.
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
"""
import json
import ssl
import urllib.request
import urllib.error
import urllib.parse
site = input('Enter URL: ')
# Short circuit to default URL if none is entered
site = site or 'http://py4e-data.dr-chuck.net/comments_42.json'
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
file_list = urllib.request.urlopen(site, context=ctx).read()
json_list = json.loads(file_list)
comments = json_list['comments']
total = 0
for comment in comments:
total += comment['count']
print(total)