Python命名空间xml解析错误

Python命名空间xml解析错误,python,Python,以下代码有什么问题?我期待boogie作为输出 import urllib.request from html.parser import HTMLParser import xml.etree.ElementTree as ET html = '''<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:enc="http://pu

以下代码有什么问题?我期待boogie作为输出

import urllib.request
from html.parser import HTMLParser
import xml.etree.ElementTree as ET

html = '''<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns="http://purl.org/rss/1.0/"
 xmlns:enc="http://purl.oclc.org/net/rss_2.0/enc#"
><foo><title>boogie</title></foo></rdf:RDF>'''

root = ET.fromstring(html)
ns = { 'default': 'http://purl.org/rss/1.0/', 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}

titles = root.findall("default:.//title", ns)
[print(title.text) for title in titles]
导入urllib.request
从html.parser导入HTMLParser
将xml.etree.ElementTree作为ET导入
html=''boogie''
root=ET.fromstring(html)
ns={‘默认值’:'http://purl.org/rss/1.0/','rdf':'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}
titles=root.findall(“默认值:.//title”,ns)
[标题中标题的打印(title.text)]
导入urllib.request
从html.parser导入HTMLParser
将xml.etree.ElementTree作为ET导入
html=''boogie''
root=ET.fromstring(html)
ns={http://purl.org/rss/1.0/}'
titles=root.findall(“./%stitle”%ns)
打印标题[0]。文本

这是工作版本

您可以将此行:
titles=root.findall(“默认值:.//title”,ns)
更改为
titles=root.findall(“.//default:title,ns)
,然后它就可以工作了。您将
default
放错了位置
import urllib.request
from html.parser import HTMLParser
import xml.etree.ElementTree as ET

html = '''<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns="http://purl.org/rss/1.0/"
 xmlns:enc="http://purl.oclc.org/net/rss_2.0/enc#"
 ><foo><title>boogie</title></foo></rdf:RDF>'''

root = ET.fromstring(html)
ns = '{http://purl.org/rss/1.0/}'

titles = root.findall(".//%stitle" % ns)
print titles[0].text