【问题标题】:HtmlAgilityPack query returning no valueHtmlAgilityPack 查询没有返回值
【发布时间】:2013-06-18 08:11:20
【问题描述】:

苦苦挣扎了 2 天。 我在 .NET 4.5 winforms 项目中使用 C# 和 HtmlAgilityPack 从网站中提取数据(我要提取的字段是 $ 流和 B/S 比率)。 我到达现场(流量:/n/t/t/t;而不是流量 245 M)但我没有任何价值。 当我在网页中看到值时,我不知道为什么我在查询时没有得到任何值。想看看别人有没有找到我的查询结果nodes=null的原因。 这是查询网页的网址:http://finance.avafin.com/tradeFlow?type=BS_RATIO&date=06%2F14%2F2013&alertId=0&symbol=spy&sectorId=0&industryId=0

我使用上面的 url 作为查询。

请注意,我使用了以下方法,但在另一个网页上使用了不同的查询并且它有效,有一些不适用于当前查询,或者我怀疑此当前网页的字段混淆了。

使用方法:

     /// <summary>
        ///     Gets the data.
        /// </summary>
        /// <param name="url"> The URL. </param>
        /// <returns> </returns>
        public List<string> GetFlowData(string url)
        {
            // ('//a[contains(@href, "genre")]')
            // <td class=" sorting_1">137.27B</td>
            //*[@id="tf_data"]/tbody/tr[1]/td[8] // this is the xpath as seen in navigator for first value => I get no value when used as a query  => (nodes = null)
            //*[@id="tf_data"]/tbody/tr[1]/td[9] //  this is the xpath as seen in navigator for second value => I get no value when used as a query => (nodes = null)

// //td[@class=''] => nodes null too


            // I see the b/s ratio node in body but no value /n/ttt instead using [@id='tf_data']/tbody
            var nodes = LoadHtmlDoc(url, "//*[@id='tf_data']/tbody");
            List<string> tickers = new List<string>();
            if (nodes == null)
            {
                return new List<string> { "Ticker not available" };
            }
            int i = 0;
            foreach (var v in nodes)
            {
                i++;

                    MessageBox.Show(v.InnerText + " " + i.ToString());
                //// The placement of the data containing bought/sold ratio
                //if (i == 7)
                //{
                //    tickers.Add(v.InnerText);
                //}
                //// The placement of the data containing $ Flow
                //if (i == 8)
                //{
                //    tickers.Add(CleanFlowData(v.InnerText));
                //}
            }

            return tickers;
        }

【问题讨论】:

  • 我做了loadhtml,它工作正常,它正确加载了html文档

标签: c# html-agility-pack


【解决方案1】:

您正在查询的页面不包含 ID 为 th_data 的表中的任何数据。如果您检查页面标记,您会看到:

<table cellpadding="0" cellspacing="0" border="0" class="display" id="tf_data">
    <thead>
        <tr height="10">
            <th align="center"></th>
            <th align="center" width="90">CHART</th>
            <th align="left" width="70">SYMBOL</th>
            <th align="left">MARKET CAP</th>
            <th align="right" width="65">PRICE</th>
            <th align="center" width="80">CHANGE</th>
            <th align="right">VOL</th>
            <th align="right">B/S RATIO</th>
            <th align="right" width="80">NET CASH FLOW</th>
        </tr>
    </thead>
    <tbody> <-- empty!
    </tbody>
</table>

在加载文档后,浏览器通过 Java Script 将所有数据添加到此表中(请参阅$(document).ready 函数)。因此,如果您从该 url 获取 html,则在浏览器运行 Java Script 代码之前将没有数据。 IE。没有什么可以解析的。

我建议您检查将 JSON 数据加载到页面中的脚本,然后简单地从您的代码中调用相同的服务。


它的范围没有问题,但要检索数据,您可以使用来自System.Net.Http 程序集的HttpClient 类。这是使用示例(由您来分析查询字符串的组成方式):

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://finance.avafin.com");
string url = "data?sEcho=2&iColumns=9&sColumns=&iDisplayStart=0&iDisplayLength=20&mDataProp_0=0&mDataProp_1=1&mDataProp_2=2&mDataProp_3=3&mDataProp_4=4&mDataProp_5=5&mDataProp_6=6&mDataProp_7=7&mDataProp_8=8&sSearch=&bRegex=false&sSearch_0=&bRegex_0=false&bSearchable_0=true&sSearch_1=&bRegex_1=false&bSearchable_1=true&sSearch_2=&bRegex_2=false&bSearchable_2=true&sSearch_3=&bRegex_3=false&bSearchable_3=true&sSearch_4=&bRegex_4=false&bSearchable_4=true&sSearch_5=&bRegex_5=false&bSearchable_5=true&sSearch_6=&bRegex_6=false&bSearchable_6=true&sSearch_7=&bRegex_7=false&bSearchable_7=true&sSearch_8=&bRegex_8=false&bSearchable_8=true&iSortCol_0=4&sSortDir_0=asc&iSortingCols=1&bSortable_0=true&bSortable_1=true&bSortable_2=true&bSortable_3=true&bSortable_4=true&bSortable_5=true&bSortable_6=true&bSortable_7=true&bSortable_8=true&type=BS_RATIO&date=06%2F14%2F2013&categoryName=&alertId=0&alertId2=&industryId=0&sectorId=0&symbol=spy&recom=&period=&perfPercent=";
var response = client.GetStringAsync(url).Result;

响应将包含您可以解析的 html。

【讨论】:

  • 好的,有什么建议可以调用 json 服务吗?我看到一些带有 '$.getJSON' 的 js 行; JsonValue.Parse(webClient.DownloadString(url); ?
  • 通过使用您的字符串 url,我能够捕获页面并提取值。但是这些值是昨天的。如何使 url 动态化以便获取当前页面?你是怎么得到网址的?我需要一个生成当前 url 的方法来刷新数据。谢谢。
  • @TeycirBenSoltane 抱歉,这超出了这个问题的范围(我认为这超出了本网站的范围)。我认为由你来调查你的服务的 API 和它需要的参数
  • 我将尝试动态修改我刚刚看到的日期并将其包含在 url 中
  • @TeycirBenSoltane 不幸的是,这个要求对于您的任务来说过于本地化,并且不适合本网站的格式(其他人不太可能需要提供日期来解析来自 finance.avafin.com 的数据)
猜你喜欢
  • 2010-11-28
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多