在以前的项目中,我从 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
和文本day
DateCreated
到目前为止,我有以下,没有运气
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")