Python 3,xml,coursera

时间:2017-07-02 20:15:13

标签: xml python-3.x

我的代码有问题。 这是开始

import urllib.request, urllib.parse, urllib.error
import xml.etree.ElementTree as ET
from urllib.request import urlopen
import ssl


ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url ='http://py4e-data.dr-chuck.net/comments_2403.html'
html = urlopen(url, context=ctx).read()
print('Retrieving', url)
uh = urllib.request.urlopen(url)
data = uh.read()
data = data.decode()
print('Retrieved', len(data), 'characters')
tree = ET.fromstring(data)

results = tree.findall('comments/comment/count')
print(tree)

这是它的输出:

Retrieving http://py4e-data.dr-chuck.net/comments_2403.html
Retrieved 3548 characters
[]
<Element 'html' at 0x02F11930>

这是什么意思?列表或数据在哪里?请帮助!!!

1 个答案:

答案 0 :(得分:0)

我修复了这段代码,现在他正在工作。问题出现了.text我忘记了这件事。

import urllib.request
import xml.etree.ElementTree as ET

url = input('Enter location:')
print ('Retrieving', url)
site = urllib.request.urlopen(url)
data = site.read().decode()
print ('Retrieved',len(data),'characters')

tree = ET.fromstring(data)
info = tree.findall('comments/comment')
lst=[]

for stuff in info:
    numbers = int(stuff.find('count').text)
    lst.append(numbers)
print('Count:',len(lst))
print("Sum:", sum(lst))