Created
August 19, 2016 14:39
-
-
Save manichabba/700da6c5a09563396d0fef580ad8f2c1 to your computer and use it in GitHub Desktop.
Extracting Data from XML: The program will prompt for a URL, read the XML data from that URL using urllib and then parse and extract the comment counts from the XML data, compute the sum of the numbers in the file.
This file contains hidden or 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 #importing urllib | |
import xml.etree.ElementTree as ET #importing xml library | |
url = raw_input("Enter the URL:") #requesting a xml file | |
#read the file and get the comment tag | |
data = urllib.urlopen(url).read() | |
tree = ET.fromstring(data) | |
lst = tree.findall('.//comment') | |
count = 0 | |
#Look through all the <comment> tags and find the <count> values sum the numbers | |
for item in lst: | |
count = count + int(item.find('count').text) | |
print count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment