0
votes

I can capture a website with the following code, but I get the following error when I try to use the HTML Agility Pack as shown in the second code snippet.

    string strURL = " http://www.donbest.com/mlb/odds/money-lines/";

        try
        {
            WebRequest req = WebRequest.Create(strURL);
            StreamReader stream = new StreamReader(req.GetResponse().GetResponseStream());
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            string strLine;

            while ((strLine = stream.ReadLine()) != null)
            {
                if (strLine.Length > 0)
                    sb.Append(strLine + Environment.NewLine);
            }
            stream.Close();
             m_strSite = sb.ToString();
             currentSiteData = m_strSite;
        }
        catch (Exception ex)
        {
            string exStr = ex.ToString();
        }

And here is the Agility Pack code.

string siteUrl = "  http://www.donbest.com/mlb/odds/money-lines";
        HtmlAgilityPack.HtmlWeb web = new HtmlWeb();
        var doc = await Task.Factory.StartNew(() => web.Load(siteUrl));

       foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table/tbody"))
        {
            Debug.WriteLine("Found: " + table.Id);
            foreach (HtmlNode row in table.SelectNodes("tr"))
            {
                Debug.WriteLine(row.InnerText.ToString());
                string tempRow = row.InnerHtml;
                foreach (HtmlNode cell in row.SelectNodes("th|td"))
                {
                    string tempCell = cell.InnerText;
                }
            }
        }

I get the error on the web.Load line.

System.AggregateException "One or more errors occurred. (An error occurred while sending the request.)"

System.AggregateException HResult=0x80131500 Message=One or more errors occurred. (An error occurred while sending the request.) Source=System.Private.CoreLib StackTrace: at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result() at HtmlAgilityPack.HtmlWeb.Get(Uri uri, String method, String path, HtmlDocument doc, IWebProxy proxy, ICredentials creds) at HtmlAgilityPack.HtmlWeb.LoadUrl(Uri uri, String method, IWebProxy proxy, ICredentials creds) at HtmlAgilityPack.HtmlWeb.Load(Uri uri, String method) at HtmlAgilityPack.HtmlWeb.Load(String url) at MLB_2019_Wagers.Views.MainPage.d__2.MoveNext() in C:\Projects2019\MLB_2019_Wagers\MLB_2019_Wagers\Views\MainPage.xaml.cs:line 233

Inner Exception 1: HttpRequestException: An error occurred while sending the request.

Inner Exception 2: COMException: The text associated with this error code could not be found.

'': Invalid characters found.

1

1 Answers

0
votes

Try this:

string siteUrl = "http://www.donbest.com/mlb/odds/money-lines";
var doc = web.Load(siteUrl);

There's extra space in your siteUrl string, and I don't think you need to start a new task for this.