簡體   English   中英

我如何解析 XML 文件,該文件要求在 python 中提供憑據

[英]How can I parse XML file which ask for credentials in python

有人可以在這里提供幫助嗎,我想解析一個 XML 文件,該文件要求使用我的代碼進行身份驗證(用戶,密碼)。 有人可以幫助我在代碼中使用或編輯什么,以便我的代碼可以解析 svn https 鏈接嗎?

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")

我認為您的代碼中有縮進錯誤。

users 和 for 循環應該出現在 function 中才能執行。

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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM