2016-02-25 2 views
0

Я пытаюсь извлечь все item элементов из файла XML в https://feeds.finance.yahoo.com/rss/2.0/headline?s=goog&region=US&lang=en-US, так что я могу получить доступ к title и link для каждого элемента, который можно затем выполнять некоторые другие функции.
не может извлечь предмет элемента из XML

XML-имеет следующую структуру:

<rss> 
    <channel> 
    <title> </title> 
    <copyright></copyright> 
    <link></link> 
    <description></description> 
    <language></language> 
    <lastBuildDate></lastBuildDate> 
    <image> 
    <url></url> 
    <title></title> 
    <link></link> 
    <width></width> 
    <height></height> 
    </image> 
    <item> 
     <title></title> 
     <link></link> 
     <description></description> 
     <guid></guid> 
     <pubDate></pubDate> 
    </item> 
    </channel> 
</rss> 


я написал следующий код:

import urllib 
from xml.etree import ElementTree 


class News(): 

    base_url = 'http://finance.yahoo.com/rss/headline?s=' 
    query = 'goog' 

    url = base_url + query 
    response = urllib.urlopen(url) 
    data = response.read() 

    dom = ElementTree.fromstring(data) 
    items = dom.findall('channel/item/') 


    for item in items: 
     print item.text 


, который выводит каждый элемент внутри <channel> элемента, например,

Google funds 128 news projects in Europe 
http://us.rd.yahoo.com/finance/news/rss/story/*http://sg.finance.yahoo.com/news/google-funds-128-news-projects-211927426.html 
None 
yahoo_finance/2067775856 
Wed, 24 Feb 2016 21:19:27 GMT 


Однако, я не могу работать, как получить доступ к элементам внутри <item> элемента. Я попытался следующий код:

for item in items: 

     title = item.find('title') 
     print title.text 

Но я получаю следующее сообщение об ошибке AttributeError: 'NoneType' object has no attribute 'text'

Как я могу получить доступ к title и link элементов внутри item элемента? Спасибо

+0

Вы можете исправить свой отступ в формате XML для лучше понять его структуру? – Arman

+0

сделано! думаю, что это правильно, спасибо –

ответ

1

Удаление косой черты в dom.findAll ('channel/item') сделал трюк. Пример кода просто выводит название

import urllib 
from xml.etree import ElementTree 


class News(): 

    base_url = 'http://finance.yahoo.com/rss/headline?s=' 
    query = 'goog' 

    url = base_url + query 
    response = urllib.urlopen(url) 
    data = response.read() 

    dom = ElementTree.fromstring(data) 
    items = dom.findall('channel/item') 


    for item in items: 
     print(item.find('title').text) 

Выход только название:

Google launches 'Accelerated Mobile Pages' feature in India 
The Death of Oscar Trivia 
Meet Atlas, Boston Dynamics' New Humanoid Robot 
[$$] Business Watch 
Google Fiber Heads To San Francisco; Faster Search Service Coming 
U.S. Justice Dept., Silicon Valley discuss online extremism 
Google Fiber to Expand to Tech Hub 
Behind Google's Deepmind Healthcare App 
Google Renews Push for ‘Fair Use’ of APIs Before Oracle Trial 
Forget Keyboards: We Dictated This Story on Google Docs 
U.S. aviation regulator starts rule-making process for public drone flights 
Android N could stand for No App Drawer: Why that's an epic mistake 
Google is putting its video streaming gadget directly inside TVs 
These Google Maps glitches are the stuff of nightmares 
Google launches AMP for faster web page loading 
Microsoft to buy app-development startup Xamarin 
Will Users Like Facebook’s New Selection of ‘Reactions?’ — Tech Roundup 
France Says Google Owes 1.6 Billion Euros in Back Taxes 
Google speeds news to smartphones, challenging Facebook 
Google funds 128 news projects in Europe 
+0

ха-ха! Спасибо! : D –

0

Вы можете попробовать это:

root = lxml.fromstring(data) 
results = root.findall('channel/item/') 
texts = [r.find('title').text for r in results] 
0

Вот несколько иной подход с использованием LXML/XPath:

import requests 
import lxml.etree 

r = requests.get('https://feeds.finance.yahoo.com/rss/2.0/headline?s=goog&region=US&lang=en-US') 
tree = lxml.etree.fromstring(r.content) 

items = tree.xpath('//item') 

def fst(i): 
    if i: return i[0] 
    else: return '' 

data = [] 
for i in items: 
    entry = { 
     'title' : fst(i.xpath('title/text()')), 
     'link' : fst(i.xpath('link/text()')), 
     'guid' : fst(i.xpath('guid/text()')), 
     'pubDate' : fst(i.xpath('pubDate/text()')), 
     'description' : fst(i.xpath('description/text()')), 
    } 
    data.append(entry) 

for entry in data: 
    print entry['title'] 
Смежные вопросы