在Elemen中找不到元素

2024-09-30 12:23:35 发布

您现在位置:Python中文网/ 问答频道 /正文

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来执行这个操作(当然,如果可能的话)


Tags: httpsresttreehttpncbinlmresxml

热门问题