Python从xhtml:link中删除属性vale
我正在尝试从href属性中收集值。我以前做过这个,但似乎无法让它在“xhtml:link”中工作 我试过下面这句话:Python从xhtml:link中删除属性vale,python,xml,python-3.x,Python,Xml,Python 3.x,我正在尝试从href属性中收集值。我以前做过这个,但似乎无法让它在“xhtml:link”中工作 我试过下面这句话: import xml.etree.ElementTree as ET root = ET.parse('items.xml').getroot() for type_tag in root.findall('xhtml:link'): value = type_tag.get('href') print(value) 还有xml <?xml versio
import xml.etree.ElementTree as ET
root = ET.parse('items.xml').getroot()
for type_tag in root.findall('xhtml:link'):
value = type_tag.get('href')
print(value)
还有xml
<?xml version="1.0" encoding="UTF-8"?>
<url>
<loc>https://www.example.com</loc>
<xhtml:link rel="alternate" href="https://www.example.com"></xhtml:link>
<xhtml:link rel="alternate" href="https://www.example.com/"></xhtml:link>
<xhtml:link rel="alternate" href="https://www.example.com/"></xhtml:link>
<xhtml:link rel="alternate" href="https://www.example.com/"></xhtml:link>
<xhtml:link rel="alternate" href="https://www.example.com/"></xhtml:link>
<xhtml:link rel="alternate" href="https://www.example.com/"></xhtml:link>
</url>
https://www.example.com
我试图找到背后的原因,但似乎什么也找不到。任何建议都会令人惊讶,谢谢。
xhtml
是一个名称空间,它需要findall()中的选项namespaces=
首先:我必须添加xmlns:xhtml=“您的名称空间”
来运行它——可能您也拥有它
我必须在findall()中使用相同的名称空间
text=''
https://www.example.com
'''
将xml.etree.ElementTree作为ET导入
root=ET.fromstring(text)#.getroot()
对于root.findall('xhtml:link',namespaces={'xhtml':'your namespace')中的类型标记:
value=type_tag.get('href')
打印(值)
我不知道是否有忽略名称空间的函数。xhtml
是一个名称空间,它需要findall()中的选项namespaces=
首先:我必须添加xmlns:xhtml=“您的名称空间”
来运行它——可能您也拥有它
我必须在findall()中使用相同的名称空间
text=''
https://www.example.com
'''
将xml.etree.ElementTree作为ET导入
root=ET.fromstring(text)#.getroot()
对于root.findall('xhtml:link',namespaces={'xhtml':'your namespace')中的类型标记:
value=type_tag.get('href')
打印(值)
我不知道是否有忽略名称空间的函数
text = '''<?xml version="1.0" encoding="UTF-8"?>
<url xmlns:xhtml="your namespace">
<loc>https://www.example.com</loc>
<xhtml:link rel="alternate" href="https://www.example.com"></xhtml:link>
<xhtml:link rel="alternate" href="https://www.example.com/"></xhtml:link>
<xhtml:link rel="alternate" href="https://www.example.com/"></xhtml:link>
<xhtml:link rel="alternate" href="https://www.example.com/"></xhtml:link>
<xhtml:link rel="alternate" href="https://www.example.com/"></xhtml:link>
<xhtml:link rel="alternate" href="https://www.example.com/"></xhtml:link>
</url>'''
import xml.etree.ElementTree as ET
root = ET.fromstring(text)#.getroot()
for type_tag in root.findall('xhtml:link', namespaces={'xhtml':'your namespace'}):
value = type_tag.get('href')
print(value)