for elem in xmldoc.iter(tag='ArticleTitle'): # <-- *ALL* <ArticleTitle> elements
...
for subelem in xmldoc.iter(tag='ArticleId'): # <-- *ALL* <ArticleId> elements
...
ElementTree中没有只选择
<ArticleId>
<ArticleTitle>
您碰巧看到了,所以您发现的任何与该代码无关的内容。
围绕实际的XML文档构造代码(
“对于每一篇文章…”
)并使用相对搜索:
pubs = []
for pubmedArticle in xmldoc.iter(tag='PubmedArticle'):
# relative search within this <PubmedArticle>
articleTitle = pubmedArticle.find('./MedlineCitation/Article/ArticleTitle')
# always verify that there are actual results for a search
if articleTitle == None:
title = "No article title found"
else:
title = articleTitle.text
for articleId in pubmedArticle.iterfind('./PubmedData//ArticleId'):
if articleId.get("IdType") == "doi":
pubs.append({"doi": articleId.text, "title": title})
[
{'doi': '10.1177/0748233717726877', 'title': 'Malathion and dithane induce DNA damage in Vicia faba.'},
{'doi': '10.1016/j.crvi.2015.02.001', 'title': 'Impact of dual inoculation with Rhizobium and PGPR on growth and antioxidant status of Vicia faba L. under copper stress.'}
]