Nov-24-2019, 04:48 PM
Hello,
I was trying to write a program that 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.
However, I encountered an error as follow:
Traceback (most recent call last):
File "C:\py4e\File 12\12.3.py", line 6, in <module>
tree = ET.fromstring(url)
TypeError: a bytes-like object is required, not 'HTTPResponse'
Wondering what did I do wrong on line 6? Thank you!!
I was trying to write a program that 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.
However, I encountered an error as follow:
Traceback (most recent call last):
File "C:\py4e\File 12\12.3.py", line 6, in <module>
tree = ET.fromstring(url)
TypeError: a bytes-like object is required, not 'HTTPResponse'
Wondering what did I do wrong on line 6? Thank you!!
import urllib.request, urllib.parse, urllib.error import xml.etree.ElementTree as ET url=urllib.request.urlopen('http://py4e-data.dr-chuck.net/comments_305570.xml') tree = ET.fromstring(url) results = tree.findall('comments/comment') count = 0 sum = 0 for line in results: x = int(line.find('count').text) count = count + 1 sum = sum + x print ("Count:",count) print ("Sum:",sum)