如何在python中解析请求凭据的XML文件

2024-09-29 04:25:26 发布

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

Can someone help here, I want to parse a XML file which asks for authentication(user, password) but with my code. Can someone help what should I use or edit in my code so that my code can parse svn https link?

import urllib.request as url
import xml.etree.ElementTree as ET

xmlfile=url.urlopen(r'file:///D:/Python/user.html')

def fileparse(xmlfile):
   tree=ET.parse(xmlfile)
   root=tree.getroot()
   #print(root.tag)

   users={root.get("name"):[]}

   for item in root.findall("client"):
      users[root.get("name")].append(item.get("name"))
   return users


k1=input("user or client, please mention:")
if k1=='user':
   k10=input("Enter the user id you want to search for:")
   d1=fileparse(xmlfile)

   for k,v in d1.items():
      if k==k10:
         print(k,v)
elif k1=='client':
   c10=input("Enter the client name you want to search for:")
   d1=fileparse(xmlfile)

   for k,v in d1.items():
      if c10 in v:
         print(k)
else:

   print("sorry please check your input values")

Tags: tonameinclientforinputparsemy
1条回答
网友
1楼 · 发布于 2024-09-29 04:25:26

我认为你的代码中有缩进错误

用户和for循环应该存在于要执行的函数中

import urllib.request as url
import xml.etree.ElementTree as ET

xmlfile = url.urlopen(r'file:///D:/Python/user.html').read()


def fileparse(xmlfile):
    tree = ET.parse(xmlfile)
    root = tree.getroot()
    # print(root.tag)

    users = {root.get("name"): []}

    for item in root.findall("client"):
        users[root.get("name")].append(item.get("name"))
        return users

print(fileparse(xmlfile))

相关问题 更多 >