【问题标题】:Adding missing sub tags using Beautiful Soup in Python在 Python 中使用 Beautiful Soup 添加缺失的子标签
【发布时间】:2017-08-11 12:40:17
【问题描述】:

我需要从下面的 XML 中提取人名。我使用了下面的代码并得到了输出(原始)。但我什至想要丢失的物品。

第四个人的中间名丢失了,所以只提取了三个名字。

示例 XML:

<author>
    <persName>
        <forename>Esayas</forename>
         <middlename>K</middlename>
         <surname>Gudina</surname>
    <lb/>
         <marker>1*</marker>,
    </persName>
    <persName>
         <forename>Solomon</forename>
         <middlename>T</middlename>
         <surname>Amade</surname>
    <lb/>
         <marker>2</marker> ,
    </persName>
    <persName>
        <forename>Fessahaye</forename>
         <middlename>A</middlename>
         <surname>Tesfamichael</surname>
    <lb/>
         <marker>3</marker> and
    </persName>
    <persName>
         <forename>Rana</forename>
        <surname>Ram</surname>
    <lb/>
         <marker>4</marker>
    </persName>
</author>

代码:

from bs4 import BeautifulSoup as bs
import codecs

name = []

with codecs.open("D:/...../2F1472-6823-11-19.authors.tei.xml", "r", "utf-8") as infile:
    soup = bs(infile, "html5lib")      

pn = soup.find_all('persname')


for i in pn:
    try:
        if len((i.find('forename')).text) != 0:
            fn = (i.find('forename')).text
        else:
            fn =""
        if len((i.find('middlename')).text) != 0:
            mn = (i.find('middlename')).text
        else:
            mn=""
        if len((i.find('surname')).text) != 0:
            sn = (i.find('surname')).text
        else:
            sn ="" 
        name.append(fn+" "+mn+" "+sn)
    except:
        print ("")

输出:

INDEX   TYPE       SIZE   VALUE
0     unicode       1     Esayas K Gudina
1     unicode       1     Solomon T Amade
2     unicode       1     Fessahaye A Tesfamichael

预期输出:

INDEX   TYPE       SIZE   VALUE
0     unicode       1     Esayas K Gudina
1     unicode       1     Solomon T Amade
2     unicode       1     Fessahaye A Tesfamichael
3     unicode       1     Rana   Ram

【问题讨论】:

  • 如果只找到中间名,你会抛出结果吗?你会认为标签是错误的,它实际上是名字吗?如果你只有一个姓怎么办?似乎你只需要决定在每种可能的情况下你想做什么,并在你的代码中考虑它。

标签: python xml python-2.7 python-3.x beautifulsoup


【解决方案1】:

试试这个:

from bs4 import BeautifulSoup as bs
import codecs

name = []

with codecs.open("tei.xml", "r", "utf-8") as infile:
    soup = bs(infile, "html5lib")
pn = soup.find_all('persname')


for i in pn:
    try:
        if i.find('forename') is not None and len(i.find('forename').text) != 0:
            fn = (i.find('forename')).text
        else:
            fn =""
        if i.find('middlename') is not None and len(i.find('middlename').text) != 0:
            mn = i.find('middlename').text
        else:
            mn=""
        if i.find('surname') is not None and  len(i.find('surname').text) != 0:
            sn = i.find('surname').text
        else:
            sn =""
        name.append(fn+" "+mn+" "+sn)
    except:
        print ("")
print (name)

输出:

[u'Esayas K Gudina', u'Solomon T Amade', u'Fessahaye A Tesfamichael', u'Rana  Ram']

【讨论】:

  • 在此处使用 try 块似乎导致的问题多于解决的问题..
  • @Aaron 你可能是对的,但我想尽可能少地更改原始海报的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 2018-09-12
  • 1970-01-01
  • 2019-02-20
  • 1970-01-01
  • 2017-02-17
  • 1970-01-01
相关资源
最近更新 更多