【问题标题】:How to filter the table contents and export the results to csv using powershell如何使用powershell过滤表格内容并将结果导出到csv
【发布时间】:2014-07-18 09:36:41
【问题描述】:

我需要使用powershell获取网页表格的选定列值。我的代码是

 $url1 = "D:\MyPowershell\Html.html"
 $ie = New-Object -com internetexplorer.application; 
$ie.visible = $true; 
$ie.navigate($url1);
while ($ie.Busy -eq $true) 
{ 
    Start-Sleep 10; 
    # $ie.refresh();

} 

$a = Get-Content "D:\MyPowershell\Html.html"

 $x=($ie.document.getElementsByTagName("tr"))`
  | where {
  ($_.innerText -match "2 - High") -and
  $_.innerText -notmatch "Work in Progress" }`
| % {
  $number, $priority, $state= $_.children | select -Expand innerText
  New-Object -Type PSObject -Property @{
    'Number' = $number
    'Priority' = $priority
    'State'   = $state

  }
} | select Number, Priority|Export-csv 'D:\Html.csv' -NoType -Delimiter "`t"

这是我创建的网页。没有其他表格网页的HTML代码是:

<html>
<head>
</head>
<body>
<table>
<thead>
<tr>
<th name="number"> Number</th>
<th name="priority">Priority</th>
<th name="state">State</th>
</tr>
</thead>
<tbody>
<tr>
<td name="check_task" class="list_checkbox ">INC0811168</td>
<td class="vt" title="" style="background-color:orange">2 - High</td>
<td style="" class="vt" title="">Assigned</td>
</tr>
<tr>
<td name="check_task" class="list_checkbox ">INC081rr68</td>
<td class="vt" title="">0 - None</td>
<td style="" class="vt" title="">Work in Progress</td>
</tr>
</tbody>
</table>
</body>
</html>

使用此编辑后的代码,我没有收到任何错误,但值未显示在 csv 中,并且仅创建了“Html”文件。相反,我在记事本中获得了一些编码文本。这里 INC0811168、'2 -High' 和 'Assigned' 是单独的字段。我需要在单独的列值之间获取带有空格的数据。我需要过滤数据并且只需要获取“INC****”和“2-High”列。检索到的数据导出到 csv。我该怎么做?

【问题讨论】:

    标签: powershell csv


    【解决方案1】:

    扩展行的内部文本会将各个字段破坏为单个字符串。改用这样的东西:

    $ie.document.getElementsByTagName("tr") | where {
      $_.innerText -match "2 - High" -and
      $_.innerText -notmatch "Work in Progress"
    } | % {
      $id, $priority, $status, $summary = $_.children | select -Expand innerText
      New-Object -Type PSObject -Property @{
        'ID'       = $id
        'Priority' = $priority
        'Status'   = $status
        'Summary'  = $summary
      }
    } | select ID, Priority | Export-Csv 'D:\Html.csv' -NoType -Delimiter "`t"
    

    用您希望 CSV 使用的任何分隔符替换 `t(制表符)。

    【讨论】:

    • 我收到错误Select-Object : Cannot process argument because the value of argument "obj" is null. Change the value of argument "o bj" to a non-null value。我已经给出了 $id、$priority 等表的 值的名称。仍然无法正常工作
    • @user3750784 “给定 $id、$priority 等表的 值的名称”是什么意思?请使用您当前拥有的代码更新您的问题,并提供示例输入数据,以便这里的人们可以重现问题。
    • 我已经在我的代码中进行了编辑,并提供了网页的表格标题和表格数据的html代码..
    • @user3750784 你在我的代码示例中的任何地方看到-or($ie.document.getElementsByTagName("th")) 吗?如果不是:你为什么要添加这个?请不要自行修改。术语(expression) -or (expression) 使结果成为布尔值,因此管道中没有要处理的行。此外,您的 HTML 示例不完整,因为没有 &lt;tr&gt;&lt;table&gt; 标记。请提供完整的表结构。
    • 当我单独使用 ($ie.document.getElementsByTagName("tr") 我得到错误Select-Object : Cannot process argument because the value of argument "obj" is null. Change the value of argument "obj" to a non-null value.
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签