ElementTree:选择正确的树?

时间:2016-11-12 20:34:40

标签: python-3.x rss

对于此处包含的数据:http://www.playonline.com/ff11us/polnews/news.xml

我有这个Python代码:

cat ~/.ssh/id_rsa.pub | pbcopy

但是它返回了一个空白数组。我选错了树吗?我是否需要选择子树或类似的东西?

试图掌握布局:

import xml.etree.ElementTree as ET
import urllib.request
rss1 = "http://www.playonline.com/ff11us/polnews/news.xml"
f = urllib.request.urlopen(rss1)
objects = ET.fromstring(f.read().decode())
print([el.attrib.get('title') for el in objects.findall('*/item')])

所以选择我认为可行的>>> for o in objects: ... print(o.tag) ... {http://purl.org/rss/1.0/}channel {http://purl.org/rss/1.0/}item {http://purl.org/rss/1.0/}item {http://purl.org/rss/1.0/}item {http://purl.org/rss/1.0/}item {http://purl.org/rss/1.0/}item ,还是需要选择整行?

我可以在item上选择获取父项,但如何从子项中获取数据(./{http://purl.org/rss/1.0/}item)?

1 个答案:

答案 0 :(得分:1)

我认为您的原始代码没有任何问题,只是它需要使用命名空间(以完全限定元素)。我在代码中添加了名称空间的哈希值。看看它是否适合你。

import xml.etree.ElementTree as etree
import urllib.request

pon_url = "http://www.playonline.com/ff11us/polnews/news.xml"
response = urllib.request.urlopen(pon_url)
xmlstr = response.read().decode()
root = etree.fromstring(xmlstr)

/* This is the namespaces. We use it later during 'findall' */
ns = {'ns1':'http://purl.org/rss/1.0/','ns2':'http://purl.org/dc/elements/1.1/'}
#etree.dump(root)

print("\nItems:\n")
print([i.text for i in root.findall('./ns1:item/ns1:title',ns)])
print("\nlinks:\n")
print([i.text for i in root.findall('./ns1:item/ns1:link',ns)])
print("\nDescriptions:\n")
print([i.text for i in root.findall('./ns1:item/ns1:description',ns)])
print("\nDates\n");
print([i.text for i in root.findall('./ns1:item/ns2:date',ns)])

<强>结果:

Items:    
['FINAL FANTASY XI Updated (Nov. 9)', 'All Worlds Maintenance (Nov. 9)', 'Temporary Suspension of NA GM Petition Service (Nov. 5)', 'Recovery from Fenrir World Technical Difficulties (Oct. 29)', 'Fenrir World Several Areas Technical Difficulties (Oct. 29)']

links:    
['http://www.playonline.com/ff11us/polnews/news24770.shtml', 'http://www.playonline.com/ff11us/polnews/news24767.shtml', 'http://www.playonline.com/ff11us/polnews/news24765.shtml', 'http://www.playonline.com/ff11us/polnews/news24762.shtml', 'http://www.playonline.com/ff11us/polnews/news24759.shtml']

Descriptions:    
['A version update was performed on FINAL FANTASY XI at the following time.<br><br>* Clients will update automatically upon launch after the date and time below. After the following time, the update will automatically begin after you press the &quot;Play&quot; button. After that, please follow the instructions on the screen.<br><br>[Date &amp; Time]<br>Nov. 9, 2016 21:00 (PST)<br><br>[Affected Service]<br>FINAL FANTASY XI<br><br>[Update Details]<br><a href="http://sqex.to/DYa" target="_blank">http://sqex.to/DYa</a>', 'At the following time, we will be performing server maintenance. During this period, FINAL FANTASY XI will be unavailable.<br><br>We apologize for any inconvenience this may cause and thank you for your patience.<br><br>* This maintenance will be accompanied by a client program version update. After maintenance is complete, the update will automatically begin after you press the &quot;Play&quot; button. After that, please follow the instructions on the screen.<br><br>* The World Transfer Service will be unavailable starting 30 minutes before the maintenance.<br><br>*After the maintenance ends, a spike in access is expected.<br>If you encounter congestion errors such as &quot;POL-1160&quot; and &quot;POL-0010&quot; during the confirmation screen or while downloading, we apologize for the inconvenience, and we ask that you try again after waiting for some time.<br><br>To ensure a smooth version update, we ask for your understanding and cooperation.<br><br>*Update details will be announced on Nov. 9, 2016 (PST)<br><br>[Date &amp; Time]<br>Nov. 9, 2016 21:00 to 23:00 (PST)<br>* Completion time is subject to change.<br><br>[Affected Service]<br>- FINAL FANTASY XI', 'At the following time, the North American GM petition service will be temporarily unavailable.<br><br>The European GM petition service will be operating normally and users may still place GM calls for urgent issues. However, please be aware that there may be significant delays until these GM calls can be answered.<br><br>We apologize for the inconvenience and thank you for your patience in this matter.<br><br>[Date &amp; Time]<br>Nov. 5, 2016 1:00 to 4:00 (PDT)<br><br>[Affected Services]<br>- The North American GM Petition service for FINAL FANTASY XI<br><br>[Cause]<br>Building Maintenance', 'At the time below, players were unable to access several areas on the Fenrir World.<br><br>We are pleased to announce that the issue has been addressed. We apologize for any inconvenience this may have caused.<br><br>[Date &amp; Time]<br>Oct. 29, 2016 from 2:58 to 4:32 (PDT)<br><br>[Details]<br>- Unable to access certain areas on the Fenrir World<br><br>[Cause]<br>- Server equipment issue<br><br>[Affected Areas]<br>Unable to access the following areas:<br>- Newton Movalpolos<br>- Abyssea - Konschtat<br>- Lufaise Meadows<br>- Monarch Linn<br>- The Garden of Ru&#39;Hmet<br>- Dynamis - Tavnazia<br>- Aydeewa Subterrane<br>- La Vaule [S]<br>- West Ronfaure<br>- North Gustaberg<br>- South Gustaberg<br>- Cape Teriggan<br>- East Sarutabaruta<br>- Ru&#39;Aun Gardens<br>- Fort Ghelsba<br>- Qulun Dome<br>- Castle Oztroja<br>- Castle Zvahl Keep [S]<br>- Sacrificial Chamber<br>- Throne Room<br>- Ranguemont Pass<br>- Ve&#39;Lugannon Palace<br>- Dynamis - Windurst<br>- Dangruf Wadi<br>- Outer Horutoto Ruins<br>- Ifrit&#39;s Cauldron<br>- Qu&#39;Bia Arena<br>- Cloister of Tremors<br>- Abyssea - Attohwa<br>- Ship bound for Selbina<br>- Jeuno-Windurst Airship<br>- Northern San d&#39;Oria<br>- Windurst Waters<br>- Lower Jeuno', 'We are currently experiencing technical difficulties with certain areas on the Fenrir World. The issue is currently under investigation, and new updates will follow as additional information becomes available.<br><br>We apologize for any inconvenience this may be causing, and we thank you for your understanding.<br><br>[Date &amp; Time]<br>From Oct. 29, 2016 2:58 (PDT)<br><br>[Affected]<br>- Certain areas on the Fenrir World<br><br>[Cause]<br>Under investigation<br><br>[Details]<br>Under investigation']

Dates    
['Thu, 10 Nov 2016 15:06:13 JST', 'Mon, 07 Nov 2016 14:14:32 JST', 'Fri, 04 Nov 2016 17:18:30 JST', 'Sat, 29 Oct 2016 20:05:44 JST', 'Sat, 29 Oct 2016 20:05:44 JST']