【问题标题】:How Can I Find a Specific Node By Using SelectSingleNode From HtmlAgilityPack如何使用 HtmlAgilityPack 中的 SelectSingleNode 查找特定节点
【发布时间】:2013-11-10 14:37:25
【问题描述】:

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

这是来自网站的 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>

这是我的代码

Dim wreq As HttpWebRequest = WebRequest.Create("http://www.nasdaq.com/symbol/goog/financials?query=income-statement&data=quarterly")
    wreq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5"
    wreq.Method = "get"
    Dim prox As IWebProxy = wreq.Proxy
    prox.Credentials = CredentialCache.DefaultCredentials
    Dim document As New HtmlAgilityPack.HtmlDocument
    Dim web As New HtmlAgilityPack.HtmlWeb
    web.UseCookies = True
    web.PreRequest = New HtmlAgilityPack.HtmlWeb.PreRequestHandler(AddressOf onPreReq)
    wreq.CookieContainer = cookies
    Dim res As HttpWebResponse = wreq.GetResponse()
    document.Load(res.GetResponseStream, True)
    Dim Page_Most_Recent_Quarter As Date = document.DocumentNode.SelectSingleNode("//*[@id='financials-iframe-wrap']/div/table//tr[2]/th[3]").InnerText

当我的代码到达最后一行时,我收到此错误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]")) 进行调试,则会返回一个空白。

我做错了什么?

【问题讨论】:

  • 为我工作。 SelectSingleNode 方法返回的是空字符串还是 null?
  • @broke 如何区分 SelectSingleNode 方法返回的是空字符串还是 null?
  • @broke 你说它对你有用。当它对你有用时,它返回了什么价值?
  • 我得到了 2013 年 9 月 30 日。有关详细信息,请参阅我的答案。

标签: vb.net html-parsing html-agility-pack selectsinglenode


【解决方案1】:

首先,你为什么要创建一个 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 dateNode As HtmlAgilityPack.HtmlNode = doc.DocumentNode.SelectSingleNode("//*[@id='financials-iframe-wrap']/div/table//tr[2]/th[3]")

        If dateNode IsNot Nothing Then
            Dim Page_Most_Recent_Quarter As Date = Convert.ToDateTime(dateNode.InnerHtml.Trim())
        End If

我试了几次,效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    相关资源
    最近更新 更多