我目前正在从一个网站获取链接作为谷歌搜索引擎。我必须通过一个按钮点击文本框中的单个关键字获取链接。我有一些方法来获取数据,这些方法将循环多次因此,我已经使用异步任务将这些方法循环多次并且工作正常。我对异步任务的问题是,当我尝试将获取的数据绑定到aspx页面中的gridview时完成第一次循环时它没有绑定
string[] cities = { "auburn", "bham", "dothan", "shoals", "huntsville", "mobile", "montgomery", "tuscaloosa" };
string[] states = { "montgomery", "delaware" };
string[] categories = { "sss", "ccc", "eee", "ggg", "hhh", "jjj", "ppp", "res", "bbb" };
protected async void btnsearch_Click(object sender, EventArgs e)
{
dt.Columns.Add("Links");
dt.Columns.Add("title");
dt.Columns.Add("keyword");
dt.Columns.Add("city");
dt.Columns.Add("state");
dt.Columns.Add("S.no");
dt.Columns.Add("linkid");
for (int s = 0; s < Statename.Count; s++)
{
//looping for state
for (int i = 0; i < dscity.Tables[0].Rows.Count; i++)
{
//looping for categories
for (int m = 0; m < 1; m++)
{
for (int w = 0; w < word.Length; w++)
{
//looping for pages
for (int j = 0; j < pagecount; )
{
if (j == 0)
{
str = "http://" + dscity.Tables[0].Rows[i]["citycode"].ToString() + "website/search/?&areaID=&catAbb=" + categories[m] + "&query=" + word[w] + "&sort=rel";
}
else
{
str = "http://" + dscity.Tables[0].Rows[i]["citycode"].ToString() + "website/search/?s=" + j + "&areaID=&catAbb=" + categories[m] + "&query=" + word[w] + "&sort=rel";
}
// Task taskA = Task.Factory.StartNew(() =>
await TaskEx.Run(() => binddata(str, dscity.Tables[0].Rows[i]["citycode"].ToString(), categories[m], word[w], Statename[s]));
j = j + 100;
if ((pagecount % 100) != 0 || pagecount > 100)
{
pagecount = ((pagecount / 100) + 1) * 100;
}
else
{
}
}
}
}
}
}
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
public void binddata(string str, string city, string category, string keyword, string statename)
{
try
{
string[] result;
WebClient client = new WebClient();
string webhtml = client.DownloadString(str);
WebRequest req = WebRequest.Create(str);
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string source = sr.ReadToEnd();
string[] location = new string[] { "<span class=\"crumb\">" };
location = source.Split(location, StringSplitOptions.RemoveEmptyEntries);
string[] location1 = new string[] { "<a href=\"/\">", "</a> ></span>" };
location1 = location[2].Split(location1, StringSplitOptions.RemoveEmptyEntries);
string state = location1[0];
string[] title1 = new string[] { "\">", "</a></span>" };
title1 = location[3].Split(title1, StringSplitOptions.RemoveEmptyEntries);
string title = title1[1];
string[] stringSeparators = new string[] { "<div class=\"content\">" };
stringSeparators = source.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
//spliting for pagecount
string[] resultcount = new string[] { "<span class=\"resultcount\">" };
resultcount = stringSeparators[0].Split(resultcount, StringSplitOptions.RemoveEmptyEntries);
if (resultcount.Length > 1)
{
string[] resultcount1 = new string[] { "</span></span>" };
resultcount = resultcount[1].Split(resultcount1, StringSplitOptions.RemoveEmptyEntries);
pagecount = Convert.ToInt32(resultcount[0]);
if (stringSeparators[1].Contains("<div class=\"toc_legend bottom\">"))
{
result = new string[] { "<div class=\"toc_legend bottom\">" };
}
else
{
result = new string[] { "<div id=\"floater\">" };
}
stringSeparators = stringSeparators[1].Split(result, StringSplitOptions.RemoveEmptyEntries);
string data = stringSeparators[0].Replace("<a class=\"gc\" href=", "");
data = data.Replace("href=\"#\"", "");
HtmlAgilityPack.HtmlDocument htmlSnippet = new HtmlAgilityPack.HtmlDocument();
htmlSnippet.LoadHtml("<!DOCTYPE html><html><head></head><body><div><div>" + data + "</body></html>");
List<string> hrefTags = new List<string>();
//bgw.RunWorkerAsync();
ExtractAllAHrefTags(htmlSnippet, city, category, title, keyword, statename);
}
}
catch (Exception ex)
{
throw (ex);
}
}
private void ExtractAllAHrefTags(HtmlAgilityPack.HtmlDocument htmlSnippet, string city, string category, string title, string keyword, string statename)
{
//bgw.RunWorkerAsync();
try
{
string ssss = string.Empty;
string temp = string.Empty;
List<string> hrefTags = new List<string>();
DataRow dr1;
var nodes1 = htmlSnippet.DocumentNode.SelectNodes("//p");
foreach (HtmlNode link in nodes1)
{
if (link.OuterHtml.Contains("=\"row\""))
{
// HtmlAttribute att =
//HtmlAttribute tag = link.ChildNodes[5].InnerHtml;
HtmlAgilityPack.HtmlDocument htmlSnippet1 = new HtmlAgilityPack.HtmlDocument();
htmlSnippet1.LoadHtml("<!DOCTYPE html><html><head></head><body> " + link.ChildNodes[5].InnerHtml + "</body></html>");
foreach (HtmlNode link1 in htmlSnippet1.DocumentNode.SelectNodes("//a[@href]"))
{
string desc = link1.InnerHtml;
HtmlAttribute att = link1.Attributes["href"];
string temp2 = att.Value.ToString();
if (temp2.Contains(".html"))
{
if (desc != "" && temp2 != "")
{
if (temp2.StartsWith("/") && temp2.Contains(".html"))
{
ssss = "http://" + city + ".website.org/" + temp2;
}
else
{
ssss = temp2;
}
dr1 = dt.NewRow();
dr1[0] = ssss;
dr1[1] = desc;
dr1[2] = keyword;
dr1[3] = city;
dr1[4] = statename;
dr1[5] = dt.Rows.Count + 1;
dr1[6] = "";
dt.Rows.Add(dr1);
}
}
}
}
}
}
catch (Exception ex)
{
}
}