【发布时间】: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