DEV Community

loading...

Day 17 of 100Daysofcode :Extracting Data From XML

iamdurga profile image Durga Pokharel ・2 min read

This is my day 17th of #100Daysofcode. Today I also continue to learned about python access on web data in Coursera..

Did many challenge in HTML regarding to the topic Introduction to the Responsive web Design challenge in Freecodecamp

Extracting Data From XML

Extensible markup language is a language that define set of rule for encoding documents in a formate that is both human readable and machine .
My code start from importing from urllib request and importing xml.etree.ElementTree as ET.

from urllib import request
import xml.etree.ElementTree as ET
Enter fullscreen mode Exit fullscreen mode

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.

url = "http://py4e-data.dr-chuck.net/comments_1113374.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)
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide