简体   繁体   English

从XML获取元素

[英]Get Element from XML

I want to get the elements from a XML that can be fetched from a URL. 我想从可以从URL提取的XML中获取元素。

I have written the following code 我写了下面的代码

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)

The XML that is available is 可用的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>

I want to get the value of submissionType 我想获取submittingType的值

When I try 当我尝试

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

I dont get any result. 我没有任何结果。 I want to get 13F-HR 我想得到13F-HR

Can someone please explain what I am doing wrong ? 有人可以解释我在做什么错吗?

Try this. 尝试这个。 If 13F-HR is your only requirement to fetch, the script should do that. 如果只需要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)

OR 要么

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)

Output: 输出:

13F-HR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM