Skip to content

Commit 5ad056c

Browse files
committed
Finished Week 5 (Parsing XML)
1 parent 2488d0e commit 5ad056c

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+
import urllib.request as ur
2+
import xml.etree.ElementTree as et
3+
4+
url = input('Enter location: ')
5+
# 'http://python-data.dr-chuck.net/comments_42.xml'
6+
7+
total_number = 0
8+
sum = 0
9+
10+
print('Retrieving', url)
11+
xml = ur.urlopen(url).read()
12+
print('Retrieved', len(xml), 'characters')
13+
14+
tree = et.fromstring(xml)
15+
counts = tree.findall('.//count')
16+
for count in counts:
17+
sum += int(count.text)
18+
total_number += 1
19+
20+
print('Count:', total_number)
21+
print('Sum:', sum)
22+
23+
24+
# ======= Outputs ======
25+
# Enter location: http://python-data.dr-chuck.net/comments_42.xml
26+
# Retrieving http://python-data.dr-chuck.net/comments_42.xml
27+
# Retrieved 4204 characters
28+
# Count: 50
29+
# Sum: 2482

0 commit comments

Comments
 (0)