繁体   English   中英

如何在PowerShell中将多个对象导出到不同的CSV列

[英]How can I export multiple objects to different CSV columns in PowerShell

我抓取了一个网站,我真正想要的是拥有一个CSV文件,该文件在单独的列中分别包含抓取的字符串。

$page = Invoke-WebRequest -Uri "SomeURL"

$obj  = $page.ParsedHtml.Body.GetElementsByTagName('SomeTag') |
        Where {$_.GetAttributeNode('class').Value -eq 'someValue'}
$obj1 = $page.ParsedHtml.Body.GetElementsByTagName('SomeAnotherTag') |
        Where {$_.GetAttributeNode('class').Value -eq 'someAnotherValue'}
$obj2 = $page.ParsedHtml.Body.GetElementsByTagName('yetAnotherTag') |
        Where {$_.getAttributeNode('class').Value -eq 'yetAnotherValue'}

$locations  = $obj.InnerText   
$locations1 = $obj1.InnerText
$locations2 = $obj2.InnerText

$toExport0 = $locations | Select-Object @{Name='locations';Expression={$_}}
$toExport1 = $locations1 | Select-Object @{Name='AnotherSetOflocations';Expression={$_}}
$toExport2 = $locations2 | Select-Object @{Name='YetAnotherSetOflocations';Expression={$_}}

$locations$locaitons1$locations2中的每个包含刮擦的字符串。 当我输出它们时,就像这样:

word
another word
yet another word
.
.
.

我想要的输出是(将其导出到以列分隔的CSV文件中):

locations,anotherSetOfLocations,YetAnotherSetOfLocations
word,...,...
another word,...,...
yet another word,...,...
.
.
.

在这些位中:

$toExport0 = $locations | select-object @{Name='locations'; Expression={$_}}

您正在创建三个单独的对象,这些对象不适用于CSV创建。 删除这些行,找到位置后立即执行所有操作。

您需要一个具有三个属性的对象:

$toExport = [PSCustomObject]@{

    'locations'       = $locations
    'anotherSetOfLoc' = $locations2
    'locations3'      = $locations3

}

$toExport | Export-Csv out.csv -NoTypeInformation

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM