-1

在以前的项目中,我从 XML 标记属性中抓取了数据,但我不知道如何获取子 XML 节点的文本。该程序从文本文件中提取 id 并将它们插入到 url 中,然后对其进行解析。XML如下:

<Article>
    <Sometag Owner="Steve" Status="online">
        <ID Version="1">231119634</PMID>
        <DateCreated>
            <Year>2012</Year>
            <Month>10</Month>
            <Day>10</Day>
        </DateCreated>

我想从子标签中获取year month和文本dayDateCreated

到目前为止,我有以下,没有运气

    link = "http://somelink.com/"+line.rstrip('\n')+"?id=xml&format=text"
    args = (curlLink + ' -L ' + link + ' -o c:\\temp.txt --proxy-ntlm -x http://myproxy:80 -k -U:') 
    sp = subprocess.Popen(args) #run curl
    sp.wait() #Wait for it to finish before proceeding
    xml_string = open(r'C:\temp.txt', 'r').read() #read in the temporary file
    os.remove(r'C:\temp.txt') # clean up
    soup = BeautifulSoup(xml_string)
    result = soup.find('DateCreated')
    if result is not None:
        date = result.children.get_text()
        g.write(date +"\n")
4

1 回答 1

3

有几种不同的方法可以从数据中获取信息:

year = int(date.Year.text)
month = int(date.Month.text)
day = int(date.Day.text)

date.text将文本内容作为字符串提供给您。你应该使用什么取决于你真正需要什么。

于 2013-07-16T20:13:15.817 回答