-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex_13json_data.py
More file actions
34 lines (27 loc) · 894 Bytes
/
ex_13json_data.py
File metadata and controls
34 lines (27 loc) · 894 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
32
33
34
# Practice with Python
# handling JSON data
import urllib.request, urllib.parse, urllib.error
import json
import ssl
url = input("Enter location: ")
try:
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
print('Retrieving', url)
# if there is no need of key to access the data
uh = urllib.request.urlopen(url, context=ctx)
data = uh.read().decode()
print('Retrieved', len(data), 'characters')
content = json.loads(data)
# 1st we limit the seach on 'comments'
# counts gives positions of the tag counts, we need to get the text from it
nums = []
for item in content['comments']:
nums.append(item['count']) # already an int
print('count', len(nums))
print(sum(nums))
except:
print("URL improperly formatted or non-existent")
exit()