File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
Using_Python_to_Access_Web_Data Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change
1
+ # prompt for a URL, read the JSON data from that URL using urllib and then parse
2
+ # and extract the comment counts from the JSON data, compute the sum of the
3
+ # numbers in the file
4
+ import urllib .request , urllib .parse ,urllib .error
5
+ import json
6
+ import ssl
7
+
8
+ #ignore cert
9
+ ctx = ssl .create_default_context ()
10
+ ctx .check_hostname = False
11
+ ctx .verify_mode = ssl .CERT_NONE
12
+
13
+ url = input ('Enter URL: ' )
14
+ if len (url )< 1 :
15
+ url = 'http://py4e-data.dr-chuck.net/comments_42.json'
16
+ fhand = urllib .request .urlopen (url ,context = ctx ).read ().decode ()
17
+ formattedJson = json .loads (fhand )
18
+ countNum = list ()
19
+
20
+ for counts in formattedJson ['comments' ]:
21
+ countNum .append (counts ['count' ])
22
+ print (countNum )
23
+ print (sum (countNum ))
You can’t perform that action at this time.
0 commit comments