File tree 1 file changed +29
-0
lines changed
Using_Python_to_Access_Web_Data
1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ # prompt for a URL, read the XML data from that URL using urllib and then parse
2
+ # and extract the comment counts from the XML data, compute the sum of the numbers
3
+ # in the file.
4
+ import urllib .request ,urllib .parse ,urllib .error
5
+ import xml .etree .ElementTree as ET
6
+ import ssl
7
+ # from bs4 import BeautifulSoup
8
+
9
+ ctx = ssl .create_default_context ()
10
+ ctx .check_hostname = False
11
+ ctx .verify_mode = ssl .CERT_NONE
12
+
13
+ url = input ('URL: ' )
14
+ if len (url )< 1 :
15
+ url = 'http://py4e-data.dr-chuck.net/comments_42.xml'
16
+ print ("Retrieving: " ,url )
17
+
18
+ xmlString = urllib .request .urlopen (url ,context = ctx ).read ()
19
+ # parsed_xml=BeautifulSoup(fhand,'html.parser')
20
+ xmlTree = ET .fromstring (xmlString )
21
+ tagList = xmlTree .findall ('comments/comment' )
22
+ # print(tagList)
23
+ count = list ()
24
+ for tag in tagList :
25
+ # print('Name: ',tag.find('name').text)
26
+ # print('Count:',tag.find('count').text)
27
+ count .append (int (tag .find ('count' ).text ))
28
+ print ('Count:' ,len (count ))
29
+ print ('Sum: ' ,sum (count ))
You can’t perform that action at this time.
0 commit comments