首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Elementtree中找不到元素

在Elementtree中找不到元素
EN

Stack Overflow用户
提问于 2019-03-05 17:19:41
回答 1查看 34关注 0票数 1
代码语言:javascript
运行
复制
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是:

代码语言:javascript
运行
复制
<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来执行(当然,如果可能的话)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-05 18:12:43

你可以试试这个

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54999299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档