Created
September 22, 2016 19:33
-
-
Save donlovett/cd88f27e3b5e8b9914de974ac5320e87 to your computer and use it in GitHub Desktop.
Final Week 5 Assignment to pull XML from Web and sum count element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
import xml.etree.ElementTree as ET | |
url = raw_input('Enter file: ') | |
#url = 'http://python-data.dr-chuck.net/comments_318170.xml' | |
mycounter = 0 | |
mysum = 0 | |
while True: | |
print 'Retrieving', url | |
uh = urllib.urlopen(url) | |
data = uh.read() | |
print 'Retrieved',len(data),'characters' | |
#print "here is the data", data | |
break | |
stuff = ET.fromstring(data) | |
lst = stuff.findall('comments/comment') | |
print 'User count:', len(lst) | |
for item in lst: | |
#print 'Name', item.find('name').text | |
#print 'Count', item.find('count').text | |
#print 'Name', item.find('name').text | |
#print 'Count', item.find('count').text | |
mycounter = mycounter + 1 | |
mysum = mysum + int(item.find('count').text) | |
print 'Count - Number of Elements', mycounter | |
print 'Sum - Count Attribute', mysum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment