-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtracting Data from JSON
More file actions
73 lines (64 loc) · 2.12 KB
/
Extracting Data from JSON
File metadata and controls
73 lines (64 loc) · 2.12 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Extracting Data from JSON
#
# In this assignment you will write a Python program somewhat similar to
#
# http://www.py4e.com/code3/json2.py.
#
# 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 and enter the sum below:
#
# We provide two files for this assignment.
# One is a sample file where we give you the sum for your testing
# and the other is the actual data you need to process for the assignment.
#
# Sample data: http://py4e-data.dr-chuck.net/comments_42.json (Sum=2553)
#
# Actual data: http://py4e-data.dr-chuck.net/comments_867382.json (Sum ends with 93)
#
# You do not need to save these files to your folder
# since your program will read the data directly from the URL.
# Note: Each student will have a distinct data url for the assignment -
# so only use your own data url for analysis.
# The data consists of a number of names and comment counts in JSON as follows:
#
# {
# comments: [
# {
# name: "Matthias"
# count: 97
# },
# {
# name: "Geomer"
# count: 97
# }
# ...
# ]
# }
#
# The closest sample code that shows how to parse JSON and extract a list is
#
# json2.py.
#
# You might also want to look at geoxml.py to see how to prompt
# for a URL and retrieve data from a URL
import urllib.request, urllib.error, urllib.parse
import json
import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
sum = 0
url = input('Enter url: ')
html = urllib.request.urlopen(url, context=ctx).read().decode()
# print(html)
# print("=====================================================")
info = json.loads(html)
# print(info)
# print('======================================================')
#print(info['comments'])
for items in info['comments']:
x = items['count']
sum = sum + x
print(sum)
# print(info['comments']["name"])