【发布时间】:2017-01-18 22:15:06
【问题描述】:
我正在尝试使用 PowerShell 从网页中提取 HTML 表格,但我无法调用表格本身。页面上有两个表,一个用于输入,另一个用于输出,理想情况下,我想检查输出表是否包含任何内容(除了表示没有结果的特定字符串),以及它是否确实提供了来自所说的信息表到文件中。
我尝试过使用Invoke-Webrequest 的ParsedHtml 属性,但这些表没有特定的元素名称或ID,也没有“类”或“标题”标签来区分两者。使用.IHTMLDocument2_all 属性确实显示了几个COMObjects(格式为TypeName: System.__ComObject#{3050f539-98b5-11cf-bb82-00aa00bdce0b}),我觉得我需要以某种方式调用以获得我需要的东西,但我不知道该怎么做。
有没有办法调用这些 COMObjects,以便我可以从它们内部提取信息?
这是我试图从中提取结果的表格的 HTML(当没有结果时):
<Center>
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=2><TR><TD>
<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=0>
<TR><TD BGCOLOR=3399FF ALIGN=CENTER><NOBR><FONT FACE="Arial" SIZE=+1><B> Search Results </B></FONT></NOBR></TD></TR>
<TR><TD><TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0>
<Center>
<table width="100%" cellpadding="5" cellspacing="0">
<tr>
<td>No assets were found for the search</td>
</tr>
</TABLE></TD></TR>
</TABLE></TD></TR>
</TABLE>
</Center>
当有结果时,有几个标题在下面显示结果,在这段代码中:
<Center>
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=2><TR><TD>
<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=0>
<TR><TD BGCOLOR=3399FF ALIGN=CENTER><NOBR><FONT FACE="Arial" SIZE=+1><B> Search Results </B></FONT></NOBR></TD></TR>
<TR><TD><TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=2 BORDER=0>
<Center>
<table width="100%" cellpadding="5" cellspacing="0">
<tr bgcolor=A9A9A9>
<th>HEADER1</th>
<th>HEADER2</th>
<th>HEADER3</th>
<th>HEADER4</th>
<th>HEADER5</th>
<th>HEADER6</th>
<th>HEADER7</th>
<th>HEADER8</th>
<th>HEADER9</th>
<th>HEADER10</th>
<th>HEADER11</th>
<th>HEADER12</th>
<th>HEADER13</th>
</tr>
<tr >
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000> </td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000> </td>
<tr>
<tr bgcolor=C0C0C0>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000> </td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000> </td>
<tr>
<tr >
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000> </td>
<td nowrap><font size= "-1" color=000000>**RESULTS**</td>
<td nowrap><font size= "-1" color=000000> </td>
<tr>
</TABLE></TD></TR>
</TABLE></TD></TR>
</TABLE>
</Center>
理想情况下,我想检查是否找到了资产,如果找到了,请将标题 1、2、3、6 和 7 下的结果提取到可用的形式(很可能是表格或 .csv 文件) )。非常感谢任何帮助。
【问题讨论】:
-
你能得到页面的 HTML 吗?您也许可以使用我在this other question 的回答来获取您正在寻找的信息。
-
能不能给个网址或者给个例子?
-
恐怕这是一个由我工作的公司设计并专门用于我工作的公司的网站,托管在我们的 Intranet 上,我无法提供完整的网站。但是,我将使用 html 的 sn-p 编辑我的问题
-
@TheMadTechnician 在发布这个问题之前我确实看过这个问题,不幸的是,除了我使用 parsedhtml 得到的唯一 comobjects 之外,我找不到任何表 ID,我终生无法找到我访问
-
你说有两张桌子。输入表的 HTML 是什么样的,它总是在结果表之前吗?是否总是有一个输入表和一个结果表(即使结果是什么都没有找到)?
标签: html powershell powershell-5.0