如何在Powershell中从RSS提要中提取特定信息?

戴夫·詹姆斯

我试图编写一个脚本,该脚本可以从仅包含用户提供的输入的网页中提取信息。

我正在使用新闻网站的独立RSS供稿进行解析。

    $url = 'http://www.independent.co.uk/news/uk/rss'
Invoke-RestMethod -Uri $url -OutFile c:\Scripts\worldnews.xml


[xml]$Content = Get-Content C:\Scripts\worldnews.xml


$Feed = $Content.rss.channel


# User Input field
$UserTerm = Read-Host 'Enter a Term'


ForEach ($msg in $Feed.Item){


   [PSCustomObject]@{


     'Source' = "News"

    'Title' = $msg.title

    'Link' = $msg.link

    'Description' = $msg.description



   }#EndPSCustomObject

  }#EndForEach

我需要添加什么,以便此脚本仅显示包含用户给出的输入的结果?例如,如果用户在用户输入中键入“ Police”,则脚本将仅显示标题中写有“ Police”的文章。

我已经尝试过if语句,但是不确定正确的语法

if(msg.title -match $UserTerm) {

我该如何工作?

自动化订单

您可以在foreach循环中的标题上执行where子句,如下所示:

$url = 'http://www.independent.co.uk/news/uk/rss'
Invoke-RestMethod -Uri $url -OutFile c:\scripts\worldnews.xml
[xml]$Content = Get-Content C:\scripts\worldnews.xml
$Feed = $Content.rss.channel

# User Input field
$UserTerm = Read-Host 'Enter a Term'
ForEach ($msg in $Feed.Item | ?{$_.Title.Contains($userTerm)})
{
    [PSCustomObject]@{
    'Source' = "News"
    'Title' = $msg.title
    'Link' = $msg.link
    'Description' = $msg.description
    }#EndPSCustomObject
}#EndForEach

如果您想执行if语句,那么您会说

if($msg.Title.Contains($userTerm))

或者您可以将-like运算符与通配符一起使用,例如

if($msg.Title -like "*$($userTerm)*")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章