学习C#网编代码心得-百度关键字查询代码
学习C#有几天了,与DELPHI对比,C#能更快、更有效的解决一些问题。
语法都大同小异,贴出一段百度关键词查询的多线程代码,以备后用。
//查询百度关键词排名多线程
private void createBDKEYThreadThread()
{
WebHtml = "";
PageNo = 1;
string key = (textBox8.Text);
WebHtml = getHttp("http://www.baidu.com/s?wd=" + key,null); //获取源码
for (int j = 0; j < 100; j++)
{
Regex r = new Regex(textBox3.Text);
Match m = r.Match(WebHtml); // 在字符串中匹配网址 //等同于DELPHI的POS()函数
if (m.Success)
{
//==============================================================================================
//找到排名-------->推广站
//==============================================================================================
string[] resultString1 = Regex.Split(WebHtml, "f EC_PP", RegexOptions.IgnoreCase); //这一块比DELPHI的分割函数更有效率的获取
for (int i = 1; i < resultString1.Length; i++)
{
Regex r1 = new Regex(textBox3.Text);
Match m1 = r1.Match(resultString1[i]);
if (m1.Success)
{
textBox7.Text ="推广站第 "+ i.ToString()+" 位";
return;
}
}
//==============================================================================================
//找到排名-------->自然排名
//==============================================================================================
string[] resultString = Regex.Split(WebHtml, "<a onmousedown=", RegexOptions.IgnoreCase);
for (int i = 1; i < resultString.Length; i++)
{
Regex r1 = new Regex(textBox3.Text);
Match m1 = r1.Match(resultString[i]);
if (m1.Success)
{
Regex reg2 = new Regex(@"'p1':(.*?),'y':'", RegexOptions.Singleline | RegexOptions.IgnoreCase); //利用正则表达式快速获取需要的内容
MatchCollection matches2 = reg2.Matches(resultString[i]);
this.textBox7.Clear();
foreach (Match match in matches2)
{
this.textBox7.Text = "排名第 " + match.Groups[1].Value.Trim() + " 位";
}
break;
}
}
}
else
{
//没有找到排名
PageNo = PageNo + 1;
label7.Text = "正在查询第" + PageNo.ToString() + "页";
WebHtml = getHttp("http://www.baidu.com/s?wd=" + textBox8.Text + "&pn=" + (PageNo * 10).ToString(),null); //获取源码
}
}
}
最新评论及回复