2012-02-14 125 views
1

試圖從Photobucket中讀取和解析RSS提要,並且我很難獲取元素的子元素。以下是RSS XML的示例:使用perl解析Photobucket RSS提要?

<?xml version="1.0" encoding="utf-8"?> 
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/"> 
    <channel> 
     <title>BlahBlah&apos;s Photobucket websitePic album media</title> 
     <description>A feed of BlahBlah&apos;s images and videos for this album</description> 
     <pubDate>Sun, 7 Aug 2011 20:11:31 MDT</pubDate> 
     <link>http://s1100.photobucket.com/albums/g409/BlahBlah/websitePic/?sort=ascending</link> 

     <lastBuildDate>Mon, 13 Feb 2012 21:04:43 MST</lastBuildDate> 
     <generator>Photobucket feed generator</generator> 
     <language>en-us</language> 
     <ttl>60</ttl> 
     <item> 
      <title>F1 sidecar</title> 

      <link>http://s1100.photobucket.com/albums/g409/BlahBlah/websitePic/?action=view&amp;current=IMG_0673.jpg&amp;sort=ascending</link> 
      <dc:creator>BlahBlah</dc:creator> 
      <description>&lt;p&gt;&lt;a href=&quot;http://s1100.photobucket.com/albums/g409/BlahBlah/&quot;&gt;BlahBlah&lt;/a&gt; posted a photo&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://s1100.photobucket.com/albums/g409/BlahBlah/websitePic/?action=view&amp;current=IMG_0673.jpg&amp;sort=ascending&quot; title=&quot;IMG_0673.jpg&quot;&gt;&lt;img src=&quot;http://i1100.photobucket.com/albums/g409/BlahBlah/websitePic/th_IMG_0673.jpg&quot; alt=&quot;IMG_0673.jpg&quot; /&gt;&lt;/a&gt;&lt;br&gt;F1 sidecar - IMG_0673.jpg&lt;/p&gt;</description> 

      <guid>http://i1100.photobucket.com/albums/g409/BlahBlah/websitePic/IMG_0673.jpg</guid> 
      <enclosure type="image/jpeg" url="http://i1100.photobucket.com/albums/g409/BlahBlah/websitePic/IMG_0673.jpg" /> 
      <media:content medium="image" type="image/jpeg" url="http://i1100.photobucket.com/albums/g409/BlahBlah/websitePic/IMG_0673.jpg"> 
       <media:title>F1 car</media:title> 
       <media:description /> 
       <media:thumbnail url="http://i1100.photobucket.com/albums/g409/BlahBlah/websitePic/th_IMG_0673.jpg" /> 
      </media:content> 
      <pubDate>Sun, 7 Aug 2011 20:11:31 MDT</pubDate> 

     </item> 

我想獲取元素以獲取其值。這裏是我的代碼,它不工作...它會遍歷所有通道項目的

use strict; 
use CGI; 
use XML::RSS; 
use LWP::Simple; 

my $test = CGI->new; 
my $url = "http://feed1100.photobucket.com/albums/g409/BlahBlah/websitePic/feed.rss"; 

my $rss = XML::RSS->new(); 
my $data = get($url); 
$rss->parse($data); 

$rss->add_module(prefix=>'media', uri=>'http://search.yahoo.com/mrss/'); 

print $test->header("text/html"); 

my $channel = $rss->{channel}; 

foreach my $item (@{ $rss->{items} }) 
{ 
    my $link = $item->{link}; 
    my $title = $item->{title}; 
    my $thumb = ''; 

    foreach my $b ({ $item->{'http://search.yahoo.com/mrss/'}->{'content'} }) 
    { 
     print "here\n"; 
     if($b->{'http://search.yahoo.com/mrss/'}->{'thumbnail'}->{'url'}) 
     { 
      $thumb = $thumb . ' ' . $b->{'http://search.yahoo.com/mrss/'}->{'thumbnail'}->{'url'}; 
     } 
    } 

    print $title, "\n", $link, "\nthumb=", $thumb, "\n\n\n"; 
} 

print $test->end_html; 

,並找到元素,但我似乎無法得到的子元素。我認爲我的語法很接近。想法?

回答

1

items事情被解析成這樣的結構:

items => [ 
    { 
    dc => { 
     creator => "BlahBlah" 
    }, 
    description => "<p><a href=\"http://s1100.photobucket.com/albums/g409/BlahBlah/\">BlahBlah</a> posted a photo</a></p><p><a href=\"http://s1100.photobucket.com/albums/g409/BlahBlah/websitePic/?action=view&current=IMG_0673.jpg&sort=ascending\" title=\"IMG_0673.jpg\"><img src=\"http://i1100.photobucket.com/albums/g409/BlahBlah/websitePic/th_IMG_0673.jpg\" alt=\"IMG_0673.jpg\" /></a><br>F1 sidecar - IMG_0673.jpg</p>", 
    enclosure => { 
     type => "image/jpeg", 
     url => "http://i1100.photobucket.com/albums/g409/BlahBlah/websitePic/IMG_0673.jpg" 
    }, 
    guid => "http://i1100.photobucket.com/albums/g409/BlahBlah/websitePic/IMG_0673.jpg", 
    "http://purl.org/dc/elements/1.1/" => { 
     creator => "BlahBlah" 
    }, 
    "http://search.yahoo.com/mrss/" => { 
     content => "\n    ...", 
     title => "F1 car" 
    }, 
    isPermaLink => "", 
    item => "\n   \n\n ...", 
    link => "http://s1100.photobucket.com/albums/g409/BlahBlah/websitePic/?action=view&current=IMG_0673.jpg&sort=ascending", 
    pubDate => "Sun, 7 Aug 2011 20:11:31 MDT", 
    title => "F1 sidecar" 
    } 
], 

比如我沒有看到"http://search.yahoo.com/mrss/"在任何"thumbnail"關鍵。轉儲數據以查看它們的外觀如何是個好主意。看到這樣的模塊,如Data::Dump

+0

這是我想要的元素,我需要元素的'url'屬性:但是,謝謝,我會嘗試數據轉儲。 – MonkeyWrench 2012-02-14 16:02:15

0

它看起來像在這條線一個錯字:

foreach my $b ({ $item->{'http://search.yahoo.com/mrss/'}->{'content'} }) 

我想你錯過了在第一個前面的「@」,「{」那裏。從你的例子

+0

如果我這樣做,然後我得到下面的錯誤不能使用字符串(「 」 ......)的數組引用,而在使用 – MonkeyWrench 2012-02-14 04:49:21

+0

‘嚴參’我不知道呢。我認爲我從未有過處理XML :: RSS使用的命名空間語法的「樂趣」。但它確實意味着'$ item-> {'http://search.yahoo.com/mrss/'} - > {'content'}'不是你可以迭代的東西。你需要找到一種不同的方式。我建議插入「use Data :: Dumper; print Dumper($ item);」就在你的foreach之上,告訴你實際上在那裏。也可能不是每個帖子都有media:content標籤,在這種情況下,您需要首先檢測。 – zostay 2012-02-14 05:00:38