由于命名空间的原因,您找不到标题元素。
以下是查找标题的示例代码:
- 从“document”标记中获取标题
- 从内部“component”标记中获取标题
import xml.etree.ElementTree as ET
import urllib.request
url = 'https://dailymed.nlm.nih.gov/dailymed/services/v2/spls/fe9e8b7d-61ea-409d-84aa-3ebd79a046b5.xml'
response = urllib.request.urlopen(url).read()
tree = ET.fromstring(response)
for docTitle in tree.findall('{urn:hl7-org:v3}title'):
print(docTitle.text)
for compTitle in tree.findall('.//{urn:hl7-org:v3}title'):
print(compTitle.text)
更新
如果您需要搜索XML节点,则应使用xPath表达式
示例:
NS = '{urn:hl7-org:v3}'
ID = '829076996'
xPathAuthorById = ''.join([
".//",
NS, "author/",
NS, "assignedEntity/",
NS, "representedOrganization/",
NS, "id[@extension='", ID,
"']/../../.."
])
xPathAuthorName = ''.join([
"./",
NS, "assignedEntity/",
NS, "representedOrganization/",
NS, "name"
])
for author in tree.findall(xPathAuthorById):
name = author.find(xPathAuthorName)
print(name.text)
这个例子打印了ID 829076996的作者名称。
更新2
你可以使用findall方法轻松处理所有assignedEntity标签。
对于每个标签,您可以拥有多个产品,因此需要另一个findall方法(请参见下面的示例)。
xPathAssignedEntities = ''.join([
".//",
NS, "author/",
NS, "assignedEntity/",
NS, "representedOrganization/",
NS, "assignedEntity/",
NS, "assignedOrganization/",
NS, "assignedEntity"
])
xPathProdCode = ''.join([
NS, "actDefinition/",
NS, "product/",
NS, "manufacturedProduct/",
NS, "manufacturedMaterialKind/",
NS, "code"
])
for assignedEntity in tree.findall(xPathAssignedEntities):
id = assignedEntity.find(NS + 'assignedOrganization/'+ NS + 'id').get('extension')
name = assignedEntity.find(NS + 'assignedOrganization/' + NS + 'name').text
for performance in assignedEntity.findall(NS + 'performance'):
actCode = performance.find(NS + 'actDefinition/'+ NS + 'code').get('displayName')
prodCode = performance.find(xPathProdCode).get('code')
print(id, '\t', name, '\t', actCode, '\t', prodCode)
这是结果:
829084545 Pfizer Pharmaceuticals LLC ANALYSIS 0049-0050
829084545 Pfizer Pharmaceuticals LLC ANALYSIS 0049-4900
829084545 Pfizer Pharmaceuticals LLC ANALYSIS 0049-4910
829084545 Pfizer Pharmaceuticals LLC ANALYSIS 0049-4940
829084545 Pfizer Pharmaceuticals LLC ANALYSIS 0049-4960
829084545 Pfizer Pharmaceuticals LLC API MANUFACTURE 0049-0050
829084545 Pfizer Pharmaceuticals LLC API MANUFACTURE 0049-4900
829084545 Pfizer Pharmaceuticals LLC API MANUFACTURE 0049-4910
829084545 Pfizer Pharmaceuticals LLC API MANUFACTURE 0049-4940
829084545 Pfizer Pharmaceuticals LLC API MANUFACTURE 0049-4960
829084545 Pfizer Pharmaceuticals LLC MANUFACTURE 0049-4900
829084545 Pfizer Pharmaceuticals LLC MANUFACTURE 0049-4910
829084545 Pfizer Pharmaceuticals LLC MANUFACTURE 0049-4960
829084545 Pfizer Pharmaceuticals LLC PACK 0049-4900
829084545 Pfizer Pharmaceuticals LLC PACK 0049-4910
829084545 Pfizer Pharmaceuticals LLC PACK 0049-4960
618054084 Pharmacia and Upjohn Company LLC ANALYSIS 0049-0050
618054084 Pharmacia and Upjohn Company LLC ANALYSIS 0049-4940
829084552 Pfizer Pharmaceuticals LLC PACK 0049-4900
829084552 Pfizer Pharmaceuticals LLC PACK 0049-4910
829084552 Pfizer Pharmaceuticals LLC PACK 0049-4960