我试图用Powershell解析非标准的rss feed

时间:2013-03-07 01:48:12

标签: parsing powershell rss pinterest

我的Pinterest提要将RSS标题截断为20个字符,我需要解析“img src”和描述末尾的较长文本。

我非常简单的代码有效,但我无法弄清楚如何让我的新标题说“你为什么要有#Elevator #Pitch - #marketing”

$feed = [xml](New-Object System.Net.WebClient).DownloadString('http://pinterest.com/bigoals365/feed.rss')

$feed.rss.channel.Item  | select title, link, description | Out-GridView

我也尝试过Invoke-WebRequest,它适用于基本的检索,但描述的解析让我感到难过。

我的Feed看起来像这样(RSS的一行):

<item><title>Why You’ve Got to Ha</title><link>http://pinterest.com/pin/329888741425045427/</link> <description> &lt;p&gt;&lt;a href="http://pinterest.com/pin/329888741425045427/"&gt;&lt; img src="http://media-cache-lt0.pinterest.com/192x/bd/5e/7c/bd5e7cd628c21313d835a4e5c89d28ee.jpg"&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt; Why You’ve Got to Have an #Elevator #Pitch - #marketing&lt;/p&gt; </description> <pubDate>Wed, 06 Mar 2013 21:59:55 +0000</pubDate><guid>http://pinterest.com/pin/329888741425045427/ </guid></item>

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

也许这样?

$feed.rss.channel.item | %{ 
   if ($_.description -match '.*<img src="([^"]+)".*<p>(.*)</p>') { 
      $_.title = $matches[2];
      $_.link = $matches[1] 
   } 
   $_ 
} | select title, link, description | Out-GridView

希望这有帮助

/弗雷德里克

答案 1 :(得分:0)

我认为这就是你要找的东西

$feed.rss.channel.Item | 
    select -Property link, description, @{
        n = 'title'
        e = {[regex]::Matches($_.description, '<p>(.+?)</p>')[1].Groups[1].Value}
     } | ogv

这使用带有n(名称)和e(表达式)键的哈希表来创建自定义属性。