使用Invoke-WebRequest进行刮擦

时间:2016-05-17 23:47:04

标签: powershell scraperwiki

我们正在将asp.net Intranet迁移到SharePoint并通过PowerShell自动执行转换。

我们只想从DIV标记中删除类名为“topnav”的链接。不是页面上的所有链接

$url = "http://intranet.company.com"
$page = Invoke-WebRequest -Uri $url
$div_topnav = $page.ParsedHtml.getElementsByTagName('div') | ? {$_.className -match 'topnav'}

这为我们提供了 topnav 的HTML,但是如何最好地从Applications节点中提取应用程序链接?我们不想要HOME或Documents节点吗?

<div class="topnav" >
<ul class="lev1 clearfix" >
    <li class="lev1 pos1 first lev1_first">
        <a href="index.html">Home</a>
    </li>
    <li class="lev1 pos2 haschildren lev1_haschildren">
        <a href="index.html">Applications</a>
        <ul>
            <li class="lev2 pos1 first lev2_first">
                <a href="http://someurl.com">App 1</a>
            </li>
            <li class="lev2 pos2 haschildren lev2_haschildren">
                <a href="index.html">Training</a>
                <ul class="lev3">
                    <li class="lev3 pos1 lev3_pos1 first lev3_first">
                        <a href="http://someurl.com">App 3</a>
                    </li>
                    <li class="lev3 pos2 lev3_pos2 last lev3_last">
                        <a href="http://someurl.com">App 4</a>
                    </li>
                </ul>
            </li>
        </ul>
    <li class="lev1 pos3 haschildren lev1_haschildren">
        <a href="index.html">Documents</a>
        <ul>
            <li class="lev2 pos1 first lev2_first">
                <a href="http://someurl.com">Doc 1</a>
            </li>
            <li class="lev2 pos2 haschildren lev2_haschildren">
                <a href="index.html">Training</a>
                <ul class="lev3">
                    <li class="lev3 pos1 lev3_pos1 first lev3_first">
                        <a href="http://someurl.com">Doc 3</a>
                    </li>
                    <li class="lev3 pos2 lev3_pos2 last lev3_last">
                        <a href="http://someurl.com">Doc 4</a>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>
</div>

1 个答案:

答案 0 :(得分:0)

我认为这就是你想要的:

[xml]$div_topnav=
@"<div class="topnav" >
    <ul class="lev1 clearfix" >
    <li class="lev1 pos1 first lev1_first">
        <a href="index.html">Home</a>
    </li>
    <li class="lev1 pos2 haschildren lev1_haschildren">
        <a href="index.html">Applications</a>
        <ul>
            <li class="lev2 pos1 first lev2_first">
                <a href="http://someurl.com">App 1</a>
            </li>
            <li class="lev2 pos2 haschildren lev2_haschildren">
                <a href="index.html">Training</a>
                <ul class="lev3">
                    <li class="lev3 pos1 lev3_pos1 first lev3_first">
                        <a href="http://someurl.com">App 3</a>
                    </li>
                    <li class="lev3 pos2 lev3_pos2 last lev3_last">
                        <a href="http://someurl.com">App 4</a>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
        <li class="lev1 pos3 haschildren lev1_haschildren">
            <a href="index.html">Documents</a>
            <ul>
                <li class="lev2 pos1 first lev2_first">
                    <a href="http://someurl.com">Doc 1</a>
                </li>
                <li class="lev2 pos2 haschildren lev2_haschildren">
                    <a href="index.html">Training</a>
                    <ul class="lev3">
                        <li class="lev3 pos1 lev3_pos1 first lev3_first">
                            <a href="http://someurl.com">Doc 3</a>
                        </li>
                        <li class="lev3 pos2 lev3_pos2 last lev3_last">
                            <a href="http://someurl.com">Doc 4</a>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</div>
"@
($div_topnav.GetElementsByTagName("a") | ? "#Text" -Like "App *").href

输出将是您所有应用的链接。

PowerShell无法解析您发布的$ div_topnav内容,因为第6行中的li-tag缺少关闭的li标签(我在我的代码段中修复了该标签)。

相关问题