Created
November 24, 2017 16:40
-
-
Save MichelleDalalJian/f587530b6e0a72357541f39b2022aa55 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
| from urllib import request | |
| import xml.etree.ElementTree as ET | |
| url = 'http://python-data.dr-chuck.net/comments_24966.xml' | |
| print ("Retrieving", url) | |
| html = request.urlopen(url) | |
| data = html.read() | |
| print("Retrieved",len(data),"characters") | |
| tree = ET.fromstring(data) | |
| results = tree.findall('comments/comment') | |
| icount=len(results) | |
| isum=0 | |
| for result in results: | |
| isum += float(result.find('count').text) | |
| print(icount) | |
| print(isum) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On the line (sum = (elements.find('count').text) + sum), it should be (sum = int((elements.text)) + sum) instead, and it should be in the for loop.