在Elementtree中找不到元素

时间:2019-03-05 09:19:41

标签: python xml elementtree

res = requests.get("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/" + "Water" + "/cids/XML")
tree = ET.fromstring(res.content)
CID = tree.find("CID").text

res中存在的XML是:

<IdentifierList xmlns="http://pubchem.ncbi.nlm.nih.gov/pug_rest" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="http://pubchem.ncbi.nlm.nih.gov/pug_rest https://pubchem.ncbi.nlm.nih.gov/pug_rest/pug_rest.xsd">
<CID>962</CID>
</IdentifierList>

我想检索的是962

tree.getchildren()产生[<Element '{http://pubchem.ncbi.nlm.nih.gov/pug_rest}CID' at 0x0000024606B9A098>]。为什么会中断,我需要怎么做才能解决此问题?我知道正则表达式可以轻松满足我的需求,但是我想使用ET来执行此操作(当然,如果有可能的话)。

1 个答案:

答案 0 :(得分:0)

您可以尝试

res = requests.get("https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/name/" + "Water" + "/cids/XML")
tree = ET.fromstring(res.content)
CID = tree[0].text