使用 powershell 和 htmlAgilityPack 输出集合

发布于 2024-12-29 15:08:05 字数 956 浏览 0 评论 0 原文

首先,是的,我是脚本和 powershell 的菜鸟,因为这将变得非常明显。在我尝试学习的过程中,我一直在使用这里的脚本: http://www.leeholmes.com/blog/2010/03/05/html-agility-pack-rocks-your-screen-scraping-world/

这就是我所拥有的far:

add-type -Path 'C:\Program Files (x86)\htmlAgilityPack\HtmlAgilityPack.dll'
$doc = New-Object HtmlAgilityPack.HtmlDocument 
$result = $doc.Load("C:\scipts\test.html") 
$texts = $doc.DocumentNode.SelectNodes("//table[@class='dataTable']/tbody/tr/td[1]/a[1]") 
$result = $texts | % {
$testText = $_
$Platform = $testtext.innertext
New-Object PsObject -Property @{ Platform = $Platform} | Select Platform 
}
$result | Sort Platform | out-string

这给了我我正在寻找的物品。

* 挑战 *

我试图将该输出作为单个字符串放入变量中,每个项目后跟一个逗号。例如 Item1、Item2、Item 3 等...

任何帮助或解释将不胜感激,因为我已经用 Google 搜索过自己,并且不一定支持我找到的东西。

谢谢!

First, yes I am a noob to scripting and powershell as that will become painfully obvious. In my attempts to learn, I have been working with a script from here: http://www.leeholmes.com/blog/2010/03/05/html-agility-pack-rocks-your-screen-scraping-world/

This is what I have thus far:

add-type -Path 'C:\Program Files (x86)\htmlAgilityPack\HtmlAgilityPack.dll'
$doc = New-Object HtmlAgilityPack.HtmlDocument 
$result = $doc.Load("C:\scipts\test.html") 
$texts = $doc.DocumentNode.SelectNodes("//table[@class='dataTable']/tbody/tr/td[1]/a[1]") 
$result = $texts | % {
$testText = $_
$Platform = $testtext.innertext
New-Object PsObject -Property @{ Platform = $Platform} | Select Platform 
}
$result | Sort Platform | out-string

This gives me the items I am looking for.

* The Challenge *

I am trying to get that output into a variable as a single string with each item followed by a comma. for Example Item1, Item2, Item 3 etc...

Any help or explanation would be appreciated as I have Googled myself cross-eyed and don't necessarily stand the things I am finding.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2025-01-05 15:08:05

来自 powershell 帮助 (get-help about_join):

下图显示了连接运算符的语法。

<String[]> -Join <Delimiter>

要解决您的问题,您需要:

  1. 创建一个包含要连接的字符串的字符串数组:

    $arrayofstrings = $结果 |排序平台| foreach-object {$_.Platform}
    
  2. 使用逗号作为分隔符连接字符串:

    $joinedstring = $arrayofstrings -join ", "
    

$joinedstring将包含Item1, Item2, Item3

From the powershell help (get-help about_join):

The following diagram shows the syntax for the join operator.

<String[]> -Join <Delimiter>

To solve your problem, you need to:

  1. Create an array of strings containing the strings you want to join:

    $arrayofstrings = $result | Sort Platform | foreach-object {$_.Platform}
    
  2. Join the strings using a comma as the delimiter:

    $joinedstring = $arrayofstrings -join ", "
    

$joinedstringwill contain Item1, Item2, Item3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文