Не могу найти элементы в Elementtree - PullRequest
0 голосов
/ 05 марта 2019
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

XML, представленный в res:

<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 голосов
/ 05 марта 2019

Вы можете попробовать это

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
...