簡體   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