在 Elementtree 中找不到元素
Can't find elements in 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 执行此操作(当然如果可能的话)。
你可以试试这个
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
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 执行此操作(当然如果可能的话)。
你可以试试这个
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