GetStringAsync查询返回空数据

时间:2013-06-30 11:05:07

标签: c# web-scraping

我在c#.NET 4项目上使用HtmlAgilityPack,尝试从此页面获取“净现金流”数据(和其他数据): http://finance.avafin.com/tradeFlow?type=BS_RATIO&date=06%2F28%2F2013&alertId=0&symbol=spy&sectorId=0&industryId=0

代码运行良好,直到昨天(返回'净现金流'的值)。看起来我的字符串网址不再正确。 你能帮我找一下字符串url查询的修正吗?什么不使用查询?我从互联网上的某个人那里得到了查询,但不知道他是如何找到这个字符串url值的。感谢。

    /// <summary>
    ///     Gets the data.
    /// </summary>
    /// <param name="stock"> The stock. </param>
    /// <returns> </returns>
    public List<string> GetFlowData(string stock)
    {
        try
        {
            int day = Convert.ToInt32(DateTime.Now.Day.ToString());
            int month = Convert.ToInt32(DateTime.Now.Month.ToString());
            int year = Convert.ToInt32(DateTime.Now.Year.ToString());
            string date = month.ToString() + @"%2F" + day.ToString() + @"%2F" + year.ToString(); 


            HttpClient client = new HttpClient {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=" +
                date + "&categoryName=&alertId=0&alertId2=&industryId=0&sectorId=0&symbol=" + stock +
                @"&recom=&period=&perfPercent=";
            string response = null;
            try
            {
                response = client.GetStringAsync(url).Result;
            }
            catch
            {
            }


            List<string> tickers = new List<string>();
            if (response != null)
            {
                List<string> htmlValues = new List<string>(response.Split(','));

                if (!(htmlValues.Count > 4))
                {
                    return new List<string> {"Data not available"};
                }

                int i = 0;
                foreach (var v in htmlValues)
                {
                    i++;

                    if (i == 11)
                    {
                        // BS
                        tickers.Add(v.Substring(1, v.Length - 2));
                    }
                    if (i == 12)
                    {
                        string[] s = v.Split('$');
                        string[] s1 = s[1].Split('<');

                        // Flow
                        tickers.Add(s1[0]);
                    }
                }
            }

            return tickers;
        }

        catch (Exception ex)
        {
            Log.WriteLog(ex);
            return null;
        }
    }

0 个答案:

没有答案