Skip to content

Instantly share code, notes, and snippets.

@donlovett
Created September 22, 2016 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donlovett/cd88f27e3b5e8b9914de974ac5320e87 to your computer and use it in GitHub Desktop.
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
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