簡體   English   中英

如何使用htmlagility pack提取表單標簽?

[英]how to extract form tag using htmlagility pack?

我在一個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時。 錯誤在哪里:(

這是我想從中提取form html源

<!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>

由於允許表單標簽重疊,因此HAP對表單標簽的處理方式有所不同,將表單標簽視為其他元素,只需調用以下方法刪除表單標志:

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

現在,您的表單標簽將按照您期望的方式處理,您可以使用與其他標簽一起使用的方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM