Skip to content

Commit 5a44755

Browse files
Add files via upload
1 parent 56c2bf1 commit 5a44755

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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))

0 commit comments

Comments
 (0)