-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-extraction.py
39 lines (31 loc) · 1.15 KB
/
json-extraction.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
# The program will prompt for 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
import urllib.request
import json
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
while True:
try: # prompt the user for a url location and read it with urllib
url = input('Enter location: ')
data = urllib.request.urlopen(url, context=ctx).read()
break
except: # prevent invalid urls from crashing the program
print('Invalid url. Try again.')
continue
print('Retrieving', url) # let the user know retrieval was successful
print('Retrieved', len(data), 'characters') # get the total number of characters in the file
js = json.loads(data)
# Access the comment section and count them
info = js['comments']
print('Comment count:', len(info))
sum = 0
# Iterate through each comment and extract the count
for item in info:
count = item['count']
sum += int(count) # add to the total sum
print('The sum of all comments is:', sum)