PythonBeautiful Soup如何从xml文件中获取标记和文本,即使不知道所有标记的名称

2024-04-18 23:30:37 发布

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

我有这样一个xml文件:

 <a>
  <b>1</b>
  <c>2</c>
  <d>
    <e>3</e>
  </d>
</a>
<a>
  <c>4</c>
  <f value ="something">5</f>
  <g value = "other"></g>
</a>

我要一份有标签和文字的单子。 例如:

[{'b':1, 'c':2, 'e':3}, {'c':4, 'f value="something"':5, 'g value = "other"':None}]

它是一个很大的xml文件,而且不是标准的,所以我只知道<a>存在,我希望所有信息都在这个标记中。你知道吗

我已经试过漂亮的汤4,但我只能检索文本部分。你知道吗

我的代码

def ProcessXml(xmlFile):
    infile = open(xmlFile, 'r')
    contents = infile.read()
    soup = BeautifulSoup(contents,'xml')
    units = soup.find_all('a')
    unitsList = []
    for i in units:
        resultType = i.text,i.next_sibling
        resultType = resultType[0].splitlines()
        for j in resultType:
            if j == '':
                resultType.remove(j)
        unitsList.append((resultType))

    return unitsList

我的输出:

[['1','2','3'],['4','5']]

Tags: 文件inforvaluecontents标签xmlsomething
2条回答
def len_descendant(desc):
    counter = 0
    try:
        for i in desc.descendants:
            #print (i)
            if i!='' and i !='\n':
                counter += 1
    except Exception:
        pass
    return counter 

def ProcessXmlTwo(fileName):  
    infile = open(fileName, 'r')
    contents = infile.read()
    soup = BeautifulSoup(contents,'xml')
    units = soup.find_all('a')
    #print (units)
    unitsList = []
    for i in units:
        this_dict = {}
        for desc in i.descendants:
            flagNoKey = False
            if len_descendant(desc)==1 or 'value="true"/>' in str(desc) or 'value="false"/>' in str(desc):
                if desc.has_attr('value'):
                    #print (desc.name)
                    key = desc.name + " " + list(desc.attrs.keys())[0] + '=\"' + list(desc.attrs.values())[0] + '\"'
                elif 'value="true"/>' in str(desc) or 'value="false"/>' in str(desc):
                    flagNoKey = True
                    key = desc.name + " " + list(desc.attrs.keys())[0] + '=\"' + list(desc.attrs.values())[0] + '\"'
                else:
                    key = desc.name
                if flagNoKey == False:
                    this_dict[key] = desc.text
                else:
                    this_dict[key] = "None"

        unitsList.append(this_dict)

    return unitsList

这是我将使用的代码。这是@Stergios编写的代码的改编。(在python 3中工作)

这是一个非常糟糕的代码,但它确实起到了作用:

def len_descendant(desc):
    counter = 0
    try:
        for i in desc.descendants:
            if i!='' and i !='\n':
                counter += 1
    except Exception:
        pass
    return counter 

def ProcessXml():  
    infile = open("xmlfile.xml", 'r')
    contents = infile.read()
    soup = BeautifulSoup(contents,'lxml')
    units = soup.find_all('a')
    unitsList = []
    for i in units:
        this_dict = {}
        for desc in i.descendants:
            print desc, len_descendant(desc)

            try:
                if desc.has_attr('value'):
                    has_attribute = True
            except Exception:
                has_attribute = False

            if len_descendant(desc)==1 or has_attribute:
                if desc.has_attr('value'):
                    key = desc.name + " " + desc.attrs.keys()[0] + '=\"' + desc.attrs.values()[0] + '\"'
                else:
                    key = desc.name

                try:
                    value = int(desc.text)
                except Exception:
                    value = None
                this_dict[key] = value
        unitsList.append(this_dict)

    return unitsList

my_dict = ProcessXml()

结果是:

[{'c': 2, 'b': 1, 'e': 3}, {'f value="something"': 5, 'c': 4, 'g value="other"': 1}]

注意:正如MYGz所提到的,'g value = "other"'}]部分无效,因此这是我在其上尝试函数的XML文件:

<a>
  <b>1</b>
  <c>2</c>
  <d>
    <e>3</e>
  </d>
</a>
<a>
  <c>4</c>
  <f value ="something">5</f>
  <g value = "other">1</g>
</a>

相关问题 更多 >