为什么我们在这里使用“命名空间对象内容的特殊检查:媒体”?

2024-09-29 04:31:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我偶然发现了这段python代码

def parseXML(xmlfile):

# create element tree object
tree = ET.parse(xmlfile)

# get root element
root = tree.getroot()

# create empty list for news items
newsitems = []

# iterate news items
for item in root.findall('./channel/item'):

    # empty news dictionary
    news = {}

    # iterate child elements of item
    for child in item:

        # special checking for namespace object content:media    HERE
        if child.tag == '{http://search.yahoo.com/mrss/}content': #HERE
            news['media'] = child.attrib['url']                 #HERE
        else:
            news[child.tag] = child.text.encode('utf8')

    # append news dictionary to news items list
    newsitems.append(news)

# return news items list
return newsitems

有人能告诉我这是什么内容:媒体检查这里吗?我们为什么要这样做?我在互联网上找不到关于这到底是什么的意义


Tags: childtreeforhereobjectcreateitemsroot
1条回答
网友
1楼 · 发布于 2024-09-29 04:31:17

这是SOAPAPI元素。例如:

 <?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">

<soap:Body>
    <m:GetPrice xmlns:m="https://www.w3schools.com/prices">
        <m:Item>Apples</m:Item>
    </m:GetPrice>
</soap:Body>

</soap:Envelope>

在上一个XML文件中,如果要获取<Item>Apples</Item>。您必须使用:

body.find('{https://www.w3schools.com/prices}Item')

相关问题 更多 >