首页 > 解决方案 > 从 powershell 调用中获取特定文本

问题描述

我正在尝试从Invoke-WebRequest使用 powershell 中获取文本的特定部分。

以下代码:

$w = Invoke-WebRequest -Uri  "url"

$w.AllElements | where class -EQ "centerControl" | select innerText

输出以下内容:

innerText                                                                                                                            
---------                                                                                                                            
.FooterLinkCovid { font-size: 16px; color: #B6975F; } a.FooterLinkCovid:hover, a.FooterLinkCovid:active { text-decoration: underli...
Copyright © 2021  | Terms & Conditions | Security | Contact Us |  Proudly developed in South Africa. | 5.0.2038.0 ()  

所以我尝试了以下方法来获取特定的文本:

$w = $w.toString() -split "[`r`n]" | select-string "5"

我想要实现的基本上是5.0.2038.0从内部文本中获取版本号,但我似乎无法正确

标签: powershell

解决方案


这适用于您的示例文本:

$w = ".FooterLinkCovid { font-size: 16px; color: #B6975F; } a.FooterLinkCovid:hover, a.FooterLinkCovid:active { text-decoration: underli...
Copyright © 2021  | Terms & Conditions | Security | Contact Us |  Proudly developed in South Africa. | 5.0.2038.0 ()  "

$version = $w.Trim().Split(" ")[-2]
# 5.0.2038.0

您可能需要稍微调整一下才能使用实际的网页,但要从中获得正确的价值$w


推荐阅读