Patrick
Patrick

Reputation: 2781

Parse a malformed HTML with HtmlAgilityPack

I am trying to parse a HTML page but the source is malformed:

<div class=\"item column-1\">
  <h2><a href=\"/index.php">Bridgestone recebe um Volkswagen Group Award</a></h2> 
  <dl class=\"article-info\"><dt class=\"article-info-term\">Detalhes</dt><dd class=\"create\">04-08-2015</dd></dl>

<p style=\"color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; line-height: normal;\">

  <img src=\"images/Bridgestone-VWGroup.jpg\" width=\"600\" height=\"400\" alt=\"Bridgestone-VWGroup\">

</p>

<p style=\"color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; line-height: normal;\">

  A Bridgestone acaba de receber um Volkswagen Group Award como reconhecimento pelo trabalho desenvolvido enquanto fornecedor daquele grupo, um prémio atribuído a um lote restrito de fornecedores internacionais

<div class=\"css_buttons1\" style=\"min-height:40px;display: inline-block;width: 425px;\">
<div class=\"css_fb_share\" style=\"display:inline-block;\"><fb:share-button href=\"http://anossaoficina.com/index.php" type=\"button_count\"></fb:share-button></div></div>

<p class=\"readmore\">
  <a href=\"/index.php?">Continuar... Bridgestone recebe um Volkswagen Group Award</a></p>

<div class=\"item-separator\"></div>
</div>

<span class=\"row-separator\"></span>
</div>

I need to extract the second P innexText "A Bridgestone..." but HtmlAgilityPack returns "" because this tag has a start <p> but does not have the </p> to close it:

HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("http://anossaoficina.com/index.php?option=com_content&view=category&layout=blog&id=78&Itemid=474");

foreach (var matchingDiv in doc.DocumentNode.SelectNodes("//*[contains(@class,'item column-1')]"))
{
    var DescritionShort = matchingDiv.SelectSingleNode("./p[1]").InnerText;
}

Upvotes: 0

Views: 504

Answers (1)

Eser
Eser

Reputation: 12546

var web = new HtmlAgilityPack.HtmlWeb();
var doc = web.Load("http://anossaoficina.com/index.php?option=com_content&view=category&layout=blog&id=78&Itemid=474");

var DescritionShort = doc.DocumentNode
                      .SelectSingleNode("//div[@class='item column-1']//p[2]")
                      .NextSibling.InnerText;

returns

A Bridgestone acaba de receber um Volkswagen Group Award como reconhecimento pelo trabalho desenvolvido enquanto fornecedor daquele grupo, um prémio atribuído a um lote restrito de fornecedores internacionais

Upvotes: 1

Related Questions