2017-02-07 13 views
1

を使用して、私はこのurlを読み取ろうとすると、このタグの間で情報を抽出しようとしています:「identificationInfo」欠落している属性のpython

をしかし、私はこのコードを使用する場合:

import requests 
import xml.etree.ElementTree as ET 

url = "http://qldspatial.information.qld.gov.au/catalogue/rest/document?id={96BD66CE-2207-4D35-815B-0E5648C0185F}&f=xml" 

response = requests.get(url) 

xml_content = response.content 

tree = ET.fromstring(xml_content) 

for child in tree: 

    print(child.tag, child.attrib) 

が、私が得た結果にはタグの属性は含まれていません。

('{http://www.isotc211.org/2005/gmd}fileIdentifier', {}) 
('{http://www.isotc211.org/2005/gmd}language', {}) 
('{http://www.isotc211.org/2005/gmd}characterSet', {}) 
('{http://www.isotc211.org/2005/gmd}parentIdentifier', {}) 
('{http://www.isotc211.org/2005/gmd}hierarchyLevel', {}) 
('{http://www.isotc211.org/2005/gmd}contact', {}) 
('{http://www.isotc211.org/2005/gmd}dateStamp', {}) 
('{http://www.isotc211.org/2005/gmd}metadataStandardName', {}) 
('{http://www.isotc211.org/2005/gmd}metadataStandardVersion', {}) 
('{http://www.isotc211.org/2005/gmd}referenceSystemInfo', {}) 
('{http://www.isotc211.org/2005/gmd}identificationInfo', {}) 
('{http://www.isotc211.org/2005/gmd}distributionInfo', {}) 
('{http://www.isotc211.org/2005/gmd}dataQualityInfo', {}) 
('{http://www.isotc211.org/2005/gmd}metadataConstraints', {})` 

私はxmlに慣れていないので、私はこれ以上の情報を見ることができません。私は一歩足りませんか?誰かが助けることができたら、それは大いに評価されるでしょう。

+0

正確に何を取得したいですか?ただのテキスト?このデータセットの72の魚の生息地域は、2016年9月30日に発効した第120条 - 漁業法1994年およびクイーンズランド州漁業規則2008年の下で宣言されている。これはすべての魚の生息地域の境界線の複合体である。または 'identificationInfo'の' xml'ツリー? – Andersson

+0

私は本当にツリーのタグのテキストの後にありますが、私はidentificationInfoのxmlツリーを印刷することができてうれしいですが、どちらかと。 – TsvGis

+0

は実際には、ツリーから、私は必要な唯一の情報は次のとおりです。 ' 2014年9月5日 'と ' < gco:CharacterString>国立公園、スポーツ競技部 '私はこれを自動化しようとしているので、同じ構造の他のxmlからこの情報を引き出す必要があります。 – TsvGis

答えて

1

ElementTreeの代わりにminidomを使用しています。必要な値を取得するコードは次のとおりです。

from xml.dom import minidom 
import requests 

url = "http://qldspatial.information.qld.gov.au/catalogue/rest/document?id={96BD66CE-2207-4D35-815B-0E5648C0185F}&f=xml" 

response = requests.get(url) 
xml_content = response.content 
doc = minidom.parseString(xml_content) 
identification = doc.getElementsByTagName("identificationInfo")[0] 
date = identification.getElementsByTagName('gco:Date')[0].firstChild.nodeValue # "2014-09-05" 
responsible_party = identification.getElementsByTagName('CI_ResponsibleParty')[0] 
department = responsible_party.getElementsByTagName('gco:CharacterString')[0].firstChild.nodeValue # "Department of National Parks, Sport and Racing" 
+0

あなたの素晴らしい!それは素晴らしい!私は昨夜、それを成功させるために美しいスープを使用しようとしていましたが、これはサードパーティのパッケージを導入する必要があるという私の問題を解決します。 – TsvGis

関連する問題