本人只能写成这样,求大神,写出更好的!!!!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
public partial class kaijiang : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string result = null;
//下载页面
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.lecai.com/lottery/draw/");
httpRequest.Timeout = 1000000;
httpRequest.UserAgent = "Code Sample Web Client";
httpRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse respone = (HttpWebResponse)httpRequest.GetResponse();
StreamReader stream = new StreamReader(respone.GetResponseStream(), Encoding.UTF8);
result = stream.ReadToEnd();
int starindex = result.IndexOf("<table");
string sresult = result.Substring(starindex, result.Length - starindex);
//string[] yy = Regex.Split(result, "table", RegexOptions.IgnoreCase);
string[] cc = Regex.Split(sresult, "<tr", RegexOptions.IgnoreCase);
for (int k = 3; k < cc.Length; k++)
{
string[] zname = Regex.Split(cc[k], ">", RegexOptions.IgnoreCase);
//string[] nname = Regex.Split(zname[3].ToString(), "</", RegexOptions.IgnoreCase);
int nnameindex = zname[3].IndexOf('<');
string qiuname = zname[3].Substring(0, nnameindex);
if (qiuname == "体育彩票")
{
continue;
}
if (cc[k].Contains(qiuname))
{
var suangse = Regex.Split(cc[k], "<span class=\"ball", RegexOptions.IgnoreCase);
string[] ds = Regex.Split(suangse[0], ">", RegexOptions.IgnoreCase);
int nameidex = ds[3].IndexOf('<');
string name = ds[3].Substring(0, nameidex);
int pos = ds[7].IndexOf('<');
string qihaogg = ds[7].Substring(0, pos);
string[] ri = Regex.Split(ds[10], "<", RegexOptions.IgnoreCase);
//int riqipos = ri[0].IndexOf("<");
string riqi = ri[0].ToString();
//var riqi=
var haoma = "";
if (suangse.Length < 1)
{
haoma += "没有开奖结果";
}
else
{
for (int i = 1; i < suangse.Length; i++)
{
int spanindex = suangse[i].IndexOf("<");
haoma += suangse[i].Substring(4, spanindex - 4) + " ";
}
Response.Write(name + " " + qihaogg + " " + riqi + " " + haoma + "<br>");
}
}
}
}
}
用正则吧,昨天刚写了一篇文章,关于采集博客园文章的。之前跟你想的一个用string去操作,但是感觉很不方便也不灵活,后来决定学正则,然后用正则取 爽多了。附上文章地址 http://www.cnblogs.com/zery/p/3446746.html
还有一种方法,园子里有人写了一个解析HTML的程序集,会用的话比正则更快更灵活。附上工具地址http://www.cnblogs.com/Ivony/p/3447536.html
最好的办法:使用webbrowser解析,然后结构化处理Document对象,我研究这个很久了,只要你用windows,这个是最完美的解析方案。
能否写出代码。谢谢
为什么不看园子的热点
Jumony
CsQuery
...你能写出更好的代码吗?
授人以鱼不如授人以渔
因为急用,请写出更好的代码。我想参考,谢谢
使用webbrowser解析 没用过,好像也是不错的解决方案
用正则表达式写灵活
也可以学一下正则,不难的,以后用到的地方多着呢
这前也写到一个取网页的数据就是用正则匹配的。只学了几个小时就可以上手了。