PowerShell处理RSS信息

转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/

环境:Windows Server 2012 EN(解决PowerShell控制台中文乱码问题:方法

通过PowerShell处理RSS信息,直接通过Invoke-Webrequest命令获取到的内容中文乱码,原因是没有指定Encoding模式,而Invoke-Webrequest命令目前并不支持指定Encoding,所以只能把获取到的网页($url)内容保存到本地($filePath),然后从本地进行读取(根据不同方式可以选择性指定Encoding)。拿博客园的RSS页面为例,获取页面中最新博客的标题("title")。代码如下:

$url = "http://feed.cnblogs.com/blog/u/212109/rss"
$filePath = "C:UserspkmacctDesktop	est.txt"
$nodeName = "title"
Invoke-WebRequest -Uri $url -OutFile $filePath
$xmlDoc = New-Object "System.Xml.XmlDocument"  
$xmlDoc.Load($filePath)  
$xmlDoc.GetElementsByTagName($nodeName)|select '#text'

调用脚本运行结果如下:

原文地址:https://www.cnblogs.com/LanTianYou/p/5083574.html