如何通过使用来自HtmlAgilityPack的SelectSingleNode查找特定节点

html-agility-pack html-parsing selectsinglenode vb.net

使用HtmlAgilityPack我试图从这个网站上的一个节点获取文本“9/30/2013”​​: http//www.nasdaq.com/symbol/goog/financials? query = input -statement&data = quartlyly

这是网站上的HTML

<div id="financials-iframe-wrap">
<br>
<div class="nextgen thin">
<div class="table-headtag">
<div style="float:left;">
<h3 style="color:#fff;">Quarterly Income Statement (values in 000's)</h3>
</div>
<div style="float:right;">
<h3><a id="quotes_content_left_hlswitchtype" href="http://www.nasdaq.com/symbol/goog/financials?query=income-statement" style="color:#fff;">Get Annual Data</a></h3>
</div>
</div>
<div style="clear:both"></div>
<table>
<tbody><tr class="tr_BG_Color">
<th class="th_No_BG">Quarter:</th>
<th style="text-align:left;">Trend</th>
<th>3rd</th>
<th>2nd</th>
<th>1st</th>
<th>4th</th>
</tr>
<tr class="tr_BG_Color">
<th class="th_No_BG">Quarter Ending:</th>
<th></th>
<th>9/30/2013</th>
<th>6/30/2013</th>
<th>3/31/2013</th>
<th>12/31/2012</th>
</tr>

这是我的代码

<div id="financials-iframe-wrap">
<br>
<div class="nextgen thin">
<div class="table-headtag">
<div style="float:left;">
<h3 style="color:#fff;">Quarterly Income Statement (values in 000's)</h3>
</div>
<div style="float:right;">
<h3><a id="quotes_content_left_hlswitchtype" href="http://www.nasdaq.com/symbol/goog/financials?query=income-statement" style="color:#fff;">Get Annual Data</a></h3>
</div>
</div>
<div style="clear:both"></div>
<table>
<tbody><tr class="tr_BG_Color">
<th class="th_No_BG">Quarter:</th>
<th style="text-align:left;">Trend</th>
<th>3rd</th>
<th>2nd</th>
<th>1st</th>
<th>4th</th>
</tr>
<tr class="tr_BG_Color">
<th class="th_No_BG">Quarter Ending:</th>
<th></th>
<th>9/30/2013</th>
<th>6/30/2013</th>
<th>3/31/2013</th>
<th>12/31/2012</th>
</tr>

当我的代码到达最后一行时,我得到此错误Object reference not set to an instance of an object.

如果我使用Debug.WriteLine(document.DocumentNode.SelectSingleNode("//*[@id='financials-iframe-wrap']/div/table/tbody/tr[2]/th[3]"))调试Debug.WriteLine(document.DocumentNode.SelectSingleNode("//*[@id='financials-iframe-wrap']/div/table/tbody/tr[2]/th[3]")) a返回空白。

我究竟做错了什么?

热门答案

首先,为什么要创建HttpWebRequest对象?让Html Agility Pack为您做繁重的工作:

    Dim doc As New HtmlAgilityPack.HtmlDocument()

    Dim web As New HtmlAgilityPack.HtmlWeb()

    web.UseCookies = True

    doc = web.Load("http://www.nasdaq.com/symbol/goog/financials?query=income-statement&data=quarterly")

加载HtmlDocument后,我们将提取日期:

    Dim doc As New HtmlAgilityPack.HtmlDocument()

    Dim web As New HtmlAgilityPack.HtmlWeb()

    web.UseCookies = True

    doc = web.Load("http://www.nasdaq.com/symbol/goog/financials?query=income-statement&data=quarterly")

我试过几次,它完美无缺。




许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因