gpt4 book ai didi

python-3.x - 元素树 : selecting right tree?

转载 作者:行者123 更新时间:2023-12-02 03:09:51 25 4
gpt4 key购买 nike

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

我有这段 Python 代码:

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 来获取父项,但我如何获取数据(链接) 来自子项目?

最佳答案

我认为您的原始代码没有任何问题,只是它需要使用 namespace (以完全限定元素)。我将 namespace 的哈希添加到您的代码中。看看它是否适合您。

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']

关于python-3.x - 元素树 : selecting right tree?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40567366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com