0

This code can work with one of the web, but with some sites it back error messages like this, I do not know how to edit (Error in stars)

var document = webBrowser1.Document;
var documentAsIHtmlDocument3 = (mshtml.IHTMLDocument3)document.DomDocument;
var htmlString = documentAsIHtmlDocument3.documentElement.innerHTML;

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlString);

// Sử dụng node để lấy tin
HtmlNodeCollection texts = doc.DocumentNode.SelectNodes("//div[@id='footer']/p");
string kq = "";

// cho vòng lặp để lấy kết quả
foreach (var item in texts)
{
    kq += item.InnerText + Environment.NewLine;
}

richTextBox1.Text = kq;

HTML code:

<div id="divTop" >
    <div id="text-conent" style="width: 500px; float: right;"></div>
    <div id="grid" style="margin-removed 505px; height: 700px;"></div>
</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
LongNgo08
  • 7
  • 1
  • 6
  • The `code PHP:` section seems to be out of context and adds nothing to the question. – RossBille Jun 24 '15 at 01:30
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – MakePeaceGreatAgain Jun 24 '15 at 07:22

1 Answers1

0

It seems that on the pages where this is successful there exists a div with the id of footer

But on other pages where this fails no such div exists.

So it seems like your logic may need to change to make the search expression that doc.DocumentNode.SelectNodes more forgiving.

Alternatively create a few more search strings that would work if your original fails:

if(texts == null){
    texts = doc.DocumentNode.SelectNodes("some other search string");
}

etc.

RossBille
  • 1,448
  • 1
  • 17
  • 26
  • but I do not understand, when I use the back simple_html_dom obtain information in div tags, so now use DocumentNode.SelectNodes returns null ? include 'simple_html_dom.php'; if(isset($_GET['date'])) { $url = 'http://10.8.32.20/Default.aspx?Menu=' . $_GET['date']; } $html = file_get_html('http://localhost:81/curl/thu.php'); echo ''; foreach($html->find('div#divTop') as $e) echo $e->innertext . '
    '; ?>
    – LongNgo08 Jun 24 '15 at 02:04