【问题标题】:how to extract form tag using htmlagility pack?如何使用 htmlagility 包提取表单标签?
【发布时间】:2015-03-21 11:41:38
【问题描述】:

我在我的一个C# 项目中使用HtmlAgilityPack 进行抓取。我需要从网页中删除<form> 标签。我搜索了如何使用 HtmlAgilityPack 提取表单标签,但找不到答案。谁能告诉我如何使用HtmlAgilityPack 提取<form> 标签?

private void Testing()
        {
            var getHtmlWeb = new HtmlWeb();
            var document = getHtmlWeb.Load(@"http://localhost/final_project/index.php");
            HtmlNode.ElementsFlags.Remove("form");
            var aTags = document.DocumentNode.SelectNodes("//form");
            int counter = 1;
            StringBuilder buffer = new StringBuilder();
            if (aTags != null)
            {
                foreach (var aTag in aTags)
                {
                    buffer.Append(counter + ". " + aTag.InnerHtml + " - " + "\t" + "<br />");
                    counter++;
                }
            }
        }

这是我的代码示例。我正在从我的localhost 中抓取一页。 aTags 的计数为 1,因为页面上只有一个表单。但是当我使用但我的 StringBuilder 对象不包含任何 InnerHtml 形式时。错误在哪里:(

这是我要废弃的 html 源代码form

<!DOCTYPE html>
<html>
    <head>
    <!-- stylesheet section -->
    <link rel="stylesheet" type="text/css" media="all" href="./_include/style.css">

    <!-- title of the page -->
    <title>Login</title>

    <!-- PHP Section -->
    <!-- Creating a connection with database-->
     <!-- end of PHP Sectoin -->

    </head>
        <body>
            <!-- now we'll check error variable to print warning -->
                        <!-- we'll submit the data to the same page to avoid excessive pages -->
            <form action="/final_project/index.php" method="post">
                <!-- ============================== Fieldset 1 ============================== -->
                <fieldset>
                    <legend>Log in credentials:</legend>
                    <hr class="hrzntlrow" />
                        <label for="input-one"><strong>User Name:</strong></label><br />
                        <input autofocus name="userName" type="text" size="20" id="input-one" class="text" placeholder="User Name" required /><br />

                        <label for="input-two"><strong>Password:</strong></label><br />
                        <input name="password" type="password" size="20" id="input-two" class="text" placeholder="Password" required />
                </fieldset>
                <!-- ============================== Fieldset 1 end ============================== -->

                <p><input type="submit" alt="SUBMIT" name="submit" value="SUBMIT" class="submit-text" /></p>
            </form>
        </body>
</html>

【问题讨论】:

    标签: c# .net web-scraping html-agility-pack


    【解决方案1】:

    由于允许表单标签重叠,HAP 以不同方式处理它们,将表单标签视为任何其他元素只需通过调用删除表单标志:

    HtmlAgilityPack.HtmlNode.ElementsFlags.Remove("form");
    

    现在您的表单标签将按照您的预期进行处理,您可以像处理其他标签一样使用。

    【讨论】:

    • 我已经发布了我的代码示例。你能检查一下吗?
    • @ZohaibAslam 您需要在加载文档之前删除该标志,因此只需将删除标志向上移动 2 行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 2013-10-14
    • 2013-08-06
    • 2018-01-24
    相关资源
    最近更新 更多