2016-11-12 8 views
3

Для данных, содержащихся здесь: http://www.playonline.com/ff11us/polnews/news.xmlElementTree: выбор правильного дерева?

У меня есть этот 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, чтобы получить родительские элементы, но как получить данные (link) от дочерних элементов?

ответ

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

Ссылку я предоставил это XML я читаю с. – Zeno

+0

Вы пытаетесь получить новости с этой страницы ... например, _FINAL FANTASY XI Обновлено (9 ноября) _, _Все обслуживание миров (9 ноября) _, _ Временное приостановление службы петиции NA GM (5 ноября) _ ? – blackpen

+0

Да, плюс URL-адреса. – Zeno