Python xml';str';对象没有属性';文本';

Python xml';str';对象没有属性';文本';,python,xml,Python,Xml,亲爱的各位 上面是我的代码,我希望这段代码能够提取xml文档中的项目计数。但是,当我运行代码时,它会显示AttributeError:“str”对象没有属性“text” 有人能帮我吗? 谢谢大家! 您在线路上犯了一个错误 import urllib.request, urllib.parse, urllib.error import re import ssl import xml.etree.ElementTree as ET ctx=ssl.create_default_context()

亲爱的各位

上面是我的代码,我希望这段代码能够提取xml文档中的项目计数。但是,当我运行代码时,它会显示AttributeError:“str”对象没有属性“text”

有人能帮我吗? 谢谢大家!



您在线路上犯了一个错误

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

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

url = 'http://py4e-data.dr-chuck.net/comments_42.xml'
htm = urllib.request.urlopen(url, context=ctx).read()


trees = ET.fromstring(htm)
tree = trees.findall('comments/comment')

for x in tree:
    print ('Count', x.find('count'.text))
您试图访问字符串“count”的文本属性,而不是find()操作结果。正确的路线是

 print ('Count', x.find('count'.text))

print('Count',x.find('Count').text)
'Count'。text
不起作用,否。这是询问
'Count'
字符串是否具有
text
属性。您是想使用
x.find('count')。text
也许?是的!!!问题解决了!谢谢你,我明白了!问题解决了!!!非常感谢你!
print ('Count', x.find('count').text)