Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python从xhtml:link中删除属性vale_Python_Xml_Python 3.x - Fatal编程技术网

Python从xhtml:link中删除属性vale

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

我正在尝试从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 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)