我想用Powershell解析非标准的RSS源
问题描述:
我的Pinterest feed将RSS标题截断为20个字符,我需要在描述结束时解析出“img src”和更长的文本。我想用Powershell解析非标准的RSS源
我非常简单的代码工作,但我无法弄清楚如何使我的新标题说:“为什么你得有一个#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并且适用于基本检索,但描述的解析让我难堪。
我的饲料看起来像这样(的RSS的一行):
<item><title>Why You’ve Got to Ha</title><link>http://pinterest.com/pin/329888741425045427/</link> <description> <p><a href="http://pinterest.com/pin/329888741425045427/">< img src="http://media-cache-lt0.pinterest.com/192x/bd/5e/7c/bd5e7cd628c21313d835a4e5c89d28ee.jpg"></a></p><p> Why You’ve Got to Have an #Elevator #Pitch - #marketing</p> </description> <pubDate>Wed, 06 Mar 2013 21:59:55 +0000</pubDate><guid>http://pinterest.com/pin/329888741425045427/ </guid></item>
任何帮助,将不胜感激!
答
也许这样?
$feed.rss.channel.item | %{
if ($_.description -match '.*<img src="([^"]+)".*<p>(.*)</p>') {
$_.title = $matches[2];
$_.link = $matches[1]
}
$_
} | select title, link, description | Out-GridView
希望这有助于
/弗雷德里克
答
我认为这是你在找什么
$feed.rss.channel.Item |
select -Property link, description, @{
n = 'title'
e = {[regex]::Matches($_.description, '<p>(.+?)</p>')[1].Groups[1].Value}
} | ogv
它使用一个哈希表与n(名称)和E(表达)键创建一个自定义属性。
这个答案的工作最适合吸引了IMG和完整的标题。非常棒!感谢Fridden这么多!你也帮助安迪:} – bigoals365 2013-03-07 20:19:25