我可以用下面的代码捕获一个网站,但是当我尝试使用HTML敏捷包时会出现以下错误,如第二个代码片段所示。
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();
}这是敏捷包代码。
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;
}
}
}我得到了web.Load行上的错误。
System.AggregateException“发生了一个或多个错误。(发送请求时发生了错误)。”
出现了System.AggregateException HResult=0x80131500 Message=One或更多错误。(发送请求时发生错误。)System.Threading.Tasks.Task
1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result() at Source=System.Private.CoreLib StackTrace(uri uri,String方法,String path,HtmlDocument doc,IWebProxy proxy,ICredentials creds) at HtmlAgilityPack.HtmlWeb.LoadUrl(Uri uri,String方法,IWebProxy proxy,ICredentials creds)在HtmlAgilityPack.HtmlWeb.Load(Uri uri,(字符串方法)在MLB_2019_Wagers.Views.MainPage.d__2.MoveNext()的HtmlAgilityPack.HtmlWeb.Load(String url),在C:\Projects2019\MLB_2019_Wagers\MLB_2019_Wagers\Views\MainPage.xaml.cs:line 233中
内部异常1: HttpRequestException:发送请求时出错。
内部异常2: COMException:无法找到与此错误代码关联的文本。
“”:找到无效字符。
发布于 2019-03-04 23:46:48
试试这个:
string siteUrl = "http://www.donbest.com/mlb/odds/money-lines";
var doc = web.Load(siteUrl);您的siteUrl字符串中有额外的空间,我认为您不需要为此启动新任务。
https://stackoverflow.com/questions/54993260
复制相似问题