开发者

is it possible to fix the problem in HtmlAgilityPack when there is a not closed html tag?

开发者 https://www.devze.com 2022-12-14 20:45 出处:网络
well i have the following problem. the html i have is malformed and i have problems with selecting nodes using html agility pack when this is the case.

well i have the following problem.

the html i have is malformed and i have problems with selecting nodes using html agility pack when this is the case.

the code is below:

string strHtml = @"
<html>
  <div>
    <p><strong>Elem_A</strong>String_A1_2 String_A1_2</p>
    <p><strong>Elem_B</strong>String_B1_2 String_B1_2</p>
  </div>
  <div>
    <p><strong>Elem_A</strong>String_A2_2 <String_A2_2> asdas</p>
    <p><strong>Elem_B</strong>String_B2_2 String_B2_2</p>
  </div>
</html>";
HtmlAgilityPack.HtmlDocument objHtmlDocument = new HtmlAgilityPack.HtmlDocument();
objHtmlDocument.LoadHtml(strHtml);
HtmlAgilityPack.HtmlNodeCollection colnodePs = objHtmlDocument.DocumentNode.SelectNodes("//p");
List<string> lststrText = new List<string>();
foreach (HtmlAgilityPack.HtmlNode nodeP in colnodePs)
{
  lststrText.Add(nodeP.InnerHtml);
}

the problem is that String_A2_2 is enclosed in brackets.

so htmlagility pack returns 5 strings instead of 4 in the lststrText.

so is it possible to let htmlagility pack return element 3 as "<strong>Elem_A</strong>String_A2_2 <String_A2_2> asdas"?

or maybe i can do some preprocessing to close the element?

the current content of lststrText is

lststrText[0] = "<strong>Elem_A</strong>String_A1_2 String_A1_2"  
lststrText[1] = "<s开发者_如何学编程trong>Elem_B</strong>String_B1_2 String_B1_2"  
lststrText[2] = ""  
lststrText[3] = ""  
lststrText[4] = "<strong>Elem_B</strong>String_B2_2 String_B2_2"


Most html parsers try to build a working DOM, meaning dangling tags are not accepted. They will be converted, or closed in some way.

If only selecting the nodes is of importance to you, and speed and huge amounts of data is not an issue, you could grab all your <p> tags with a regular expression instead:

Regex reMatchP = new Regex(@"<(p)>.*?</\1>");
foreach (Match m in reMatchP.Matches(strHtml))
{
   Console.WriteLine(m.Value);
}

This regular expression assumes the <p> tags are well formed and closed.

If you are to run this Regex a lot in your program you should declare it as:

static Regex reMatchP = new Regex(@"<(p)>.*?</\1>", RegexOptions.Compiled);

[Edit: Agility pack change]

If you want to use HtmlAgility pack you can modify the PushNodeEnd function in HtmlDocument.cs:

if (HtmlNode.IsCDataElement(CurrentNodeName()))
{
   _state = ParseState.PcData;
   return true;
}

// new code start
if ( !AllowedTags.Contains(_currentnode.Name) )
{
    close = true;
}
// new code end

where AllowedTags would be a list of all known tags: b, p, br, span, div, etc.

the output is not 100% what you want, but maybe close enough?

<strong>Elem_A</strong>String_A1_2 String_A1_2
<strong>Elem_B</strong>String_B1_2 String_B1_2
<strong>Elem_A</strong>String_A2_2 <ignorestring_a2_2></ignorestring_a2_2> asdas
<strong>Elem_B</strong>String_B2_2 String_B2_2


You could use TidyNet to do the pre/postprocessing you allude to. Can you edit your answer to explain why that wouldnt be applicable in your case?

0

精彩评论

暂无评论...
验证码 换一张
取 消