从XML获取元素

2024-04-25 08:24:57 发布

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

我想从可以从URL获取的XML中获取元素。你知道吗

我编写了以下代码

import xml.etree.ElementTree as ET
url = 'https://www.sec.gov/Archives/edgar/data/1568219/000156821918000001/primary_doc.xml'
    req = ulib.Request(url)
    primaryXML = ulib.urlopen(req)
    xmlData = primaryXML.read()
    content = ET.fromstring(xmlData)

可用的XML是

<edgarSubmission xmlns="http://www.sec.gov/edgar/thirteenffiler" xmlns:com="http://www.sec.gov/edgar/common">
<headerData>
<submissionType>13F-HR</submissionType>
<filerInfo>
<liveTestFlag>LIVE</liveTestFlag>
<flags>
<confirmingCopyFlag>false</confirmingCopyFlag>
<returnCopyFlag>false</returnCopyFlag>
<overrideInternetFlag>false</overrideInternetFlag>
</flags>
<filer>
<credentials>
<cik>0001568219</cik>
<ccc>XXXXXXXX</ccc>
</credentials>
</filer>
<periodOfReport>12-31-2017</periodOfReport>
</filerInfo>
</headerData>
<formData>
<coverPage>
<reportCalendarOrQuarter>12-31-2017</reportCalendarOrQuarter>
<filingManager>
<name>TD Ameritrade Trust Co</name>
<address>
<com:street1>PO BOX 17748</com:street1>
<com:city>DENVER</com:city>
<com:stateOrCountry>CO</com:stateOrCountry>
<com:zipCode>80217-0748</com:zipCode>
</address>
</filingManager>
<reportType>13F HOLDINGS REPORT</reportType>
<form13FFileNumber>028-15163</form13FFileNumber>
<provideInfoForInstruction5>N</provideInfoForInstruction5>
</coverPage>
<signatureBlock>
<name>Erik Hunt</name>
<title>Senior Analyst</title>
<phone>303-294-5311</phone>
<signature>Erik Hunt</signature>
<city>Denver</city>
<stateOrCountry>CO</stateOrCountry>
<signatureDate>02-26-2018</signatureDate>
</signatureBlock>
<summaryPage>
<otherIncludedManagersCount>0</otherIncludedManagersCount>
<tableEntryTotal>20</tableEntryTotal>
<tableValueTotal>268468</tableValueTotal>
<isConfidentialOmitted>false</isConfidentialOmitted>
</summaryPage>
</formData>
</edgarSubmission>

我想得到submissionType的值

当我尝试的时候

content.find("edgarSubmission/headerData/submissionType")

我没有得到任何结果。我要13小时

有人能解释一下我做错了什么吗?你知道吗


Tags: namecomfalsecitywwwxmlsecet
1条回答
网友
1楼 · 发布于 2024-04-25 08:24:57

试试这个。如果13F-HR是获取的唯一要求,那么脚本应该这样做。你知道吗

import requests
from lxml.html import fromstring

res = requests.get("https://www.sec.gov/Archives/edgar/data/1568219/000156821918000001/primary_doc.xml")
tree = fromstring(res.content)
item = tree.cssselect("submissionType")[0].text
print(item)

或者

from urllib.request import urlopen
from lxml.html import fromstring

res = urlopen("https://www.sec.gov/Archives/edgar/data/1568219/000156821918000001/primary_doc.xml")
tree = fromstring(res.read())
item = tree.cssselect("submissionType")[0].text
print(item)

输出:

13F-HR

相关问题 更多 >