Parsing XML with namespace in Python via 'ElementTree'を介して他の質問を見て、xml.etree.ElementTreeのドキュメントを確認しました。私が持っている問題はまったく似ているので、これを重複としてタグ付けすることは自由ですが、私はそれを理解できません。名前空間でXML属性を解析するpython3
私は問題を抱えているコードの行は次のように
instance_alink = root.find('{http://www.w3.org/2005/Atom}link')
私のコードは次のとおりです。
import xml.etree.cElementTree as ET
tree = ET.parse('../../external_data/rss.xml')
root = tree.getroot()
instance_title = root.find('channel/title').text
instance_link = root.find('channel/link').text
instance_alink = root.find('{http://www.w3.org/2005/Atom}link')
instance_description = root.find('channel/description').text
instance_language = root.find('channel/language').text
instance_pubDate = root.find('channel/pubDate').text
instance_lastBuildDate = root.find('channel/lastBuildDate').text
XMLファイル:私は「
<?xml version="1.0" encoding="windows-1252"?>
<rss version="2.0">
<channel>
<title>Filings containing financial statements tagged using the US GAAP or IFRS taxonomies.</title>
<link>http://www.example.com</link>
<atom:link href="http://www.example.com" rel="self" type="application/rss+xml" xmlns:atom="http://www.w3.org/2005/Atom"/>
<description>This is a list of up to 200 of the latest filings containing financial statements tagged using the US GAAP or IFRS taxonomies, updated every 10 minutes.</description>
<language>en-us</language>
<pubDate>Mon, 20 Nov 2017 20:20:45 EST</pubDate>
<lastBuildDate>Mon, 20 Nov 2017 20:20:45 EST</lastBuildDate>
....
属性を取得しようとしている行は6行目です。そう 'のhref'、 'タイプ' など
<atom:link href="http://www.example.com" rel="self" type="application/rss+xml" xmlns:atom="http://www.w3.org/2005/Atom"/>
もちろん、私は
instance_alink = root.find('{http://www.w3.org/2005/Atom}link').attrib
を試みたが、それはNoneを入力しないだ原因、それは動作しません。私の考えは、それは子供を捜しているが、何もありません。 XMLの他の行の属性を取得できますが、何らかの理由でこれらの属性を取得することはできません。私もElementTreeとlxmlで遊んだことがあります(しかし何らかの理由でlxmlがWindows上で正しく読み込まれません)
ドキュメントが疎そうですので、何か助けてください。