首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PowerShell从网页复制文本块

使用PowerShell从网页复制文本块
EN

Stack Overflow用户
提问于 2015-12-11 21:47:36
回答 1查看 2.7K关注 0票数 2

我提取了整个网页作为文本,该文本被分配给一个变量。现在我需要选择文本的一部分,并将其赋值给另一个变量。比方说,我的课文是:

代码语言:javascript
复制
Note: Your feedback is very important to us, however, we do not
respond to individual submissions through this channel. If you require
support, please visit the Safety & Security Center. Follow: Change log
for version 1.211.2457.0 This page shows you what's changed in the
most recent definitions update for Microsoft antimalware and
antispyware software. 

You can also see changes in the last 20 updates from the Change
definition version menu on the right.

The latest update is:
1.211.2457.0
Download the latest update.

 New definitions (?)



Antimalware (Antivirus + Antispyware)

我希望将以下文本赋值给一个变量

代码语言:javascript
复制
1.211.2457.0

我现在的代码是

代码语言:javascript
复制
$URI = "http://www.example.com/mynewpage"
$HTML = Invoke-WebRequest -Uri $URI
$WebPageText = ($HTML.ParsedHtml.getElementsByTagName("div") | Where-Object{$_.className -eq "span bp0-col-1-1 bp1-col-1-1 bp2-col-1-1 bp3-col-1-1"}).innerText

我试过Select-String -SimpleMatch "The latest update is:*Download the latest update." -InputObject $WebPageText,但我很确定这是错的。我对PowerShell脚本很陌生。所以如果我漏掉了明显的东西请原谅。

提前谢谢你!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-11 22:14:15

SimpleMatch会忽略任何regex。它也不允许任何通配符。来自TechNet

使用简单匹配而不是正则表达式匹配。在简单匹配中,Select搜索模式参数中的文本输入。它不将模式参数的值解释为正则表达式语句。

您可以做的是使用regex找到一个字符串,其中的行只包含数字和句点:"^[\d\.]+$"

代码语言:javascript
复制
$version = ($WebPageText | Select-String "^[\d\.]+$").Matches.Value

可能会有更多的可能被返回,因此您可能需要说明这一点。

如果您想要一个更有针对性的(但没有保证唯一的结果),您可以只使用-match操作符。

代码语言:javascript
复制
If(($WebPageText | out-string) -match "(?sm)The latest update is:\s+(.*?)\s+Download the latest update"){
    $version = $Matches[1]
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34233076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档