如何在Service中对远程网站代码进行分析提取【高分请教】

wadsunglow 2010-08-07 11:15:45
如:
try
{
//获取远程网站代码内容
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 30000;
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("GB2312");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
string strWebContent = streamReader.ReadToEnd();

//对获取的内容进行分析
int iBodyStart = strWebContent.IndexOf("iMonitor", 0);
int iTableStart = strWebContent.IndexOf("<table", iBodyStart);
int iTableEnd = strWebContent.IndexOf("</table>", iTableStart);
string strWeb = strWebContent.Substring(iTableStart, iTableEnd - iTableStart + 8);

//生成HtmlDocument
WebBrowser webb = new WebBrowser();
webb.Navigate("about:blank");
HtmlDocument htmldoc = webb.Document.OpenNew(true);
htmldoc.Write(strWeb);
HtmlElementCollection htmlTR = htmldoc.GetElementsByTagName("TR");
DataTable dt = new DataTable("seltable");
dt.Columns.Add("ID", Type.GetType("System.String"));
dt.Columns.Add("mc", Type.GetType("System.String"));
dt.Columns.Add("dqz", Type.GetType("System.String"));
dt.Columns.Add("zt", Type.GetType("System.String"));
int i = 0;
foreach (HtmlElement tr in htmlTR)
{
i++;
if (i > 3)
{
string strID = tr.GetElementsByTagName("TD")[0].InnerText;
string strmc = tr.GetElementsByTagName("TD")[1].InnerText;
string strdqz = tr.GetElementsByTagName("TD")[2].InnerText;
string strzt = tr.GetElementsByTagName("TD")[3].InnerText;
}
}
}
catch
{
//EventLog.WriteEntry();
}

但service中用不了WebBrowser ,该如何对返回的代码进行提取?
...全文
101 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wadsunglow 2010-08-09
  • 打赏
  • 举报
回复
怎么取代码里的表格中的各行列值呢?
wadsunglow 2010-08-08
  • 打赏
  • 举报
回复
我要取的是各行列的值出来
wadsunglow 2010-08-08
  • 打赏
  • 举报
回复
string strWebContent = streamReader.ReadToEnd();
指对strWebContent中的<table>中的各行列值进行取值然后存入数据库
zehochux 2010-08-08
  • 打赏
  • 举报
回复
帮你顶了。
wadsunglow 2010-08-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 viki117 的回复:]
引用 4 楼 computerfox 的回复:
用WebClient !

C# code
System.Net.WebClient wc = new System.Net.WebClient();
Byte[] pageData = wc.DownloadData("网页地址");
string s= System.Text.Encoding.Default.GetString(pa……
[/Quote]

怎么取S里的表格中的各行列值呢?
xk1126 2010-08-08
  • 打赏
  • 举报
回复
System.Net.WebClient wc = new System.Net.WebClient();
Byte[] pageData = wc.DownloadData("网页地址");
string s= System.Text.Encoding.Default.GetString(pageData);
viki117 2010-08-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 computerfox 的回复:]
用WebClient !

C# code
System.Net.WebClient wc = new System.Net.WebClient();
Byte[] pageData = wc.DownloadData("网页地址");
string s= System.Text.Encoding.Default.GetString(pageData);
[/Quote]
这个可以...
wadsunglow 2010-08-07
  • 打赏
  • 举报
回复
HtmlDocument能在service中用么
bloodish 2010-08-07
  • 打赏
  • 举报
回复
直接New一个HtmlDocument不就可以了?
没有必要通过WebBrowser来生成Document对象

HtmlDocument doc = new HtmlDocuemnt();
doc.Write(strWeb);
cjh200102 2010-08-07
  • 打赏
  • 举报
回复
楼主说得是网络爬虫吧
捷哥1999 2010-08-07
  • 打赏
  • 举报
回复
用WebClient !
System.Net.WebClient wc = new System.Net.WebClient();
Byte[] pageData = wc.DownloadData("网页地址");
string s= System.Text.Encoding.Default.GetString(pageData);
Thr21ough 2010-08-07
  • 打赏
  • 举报
回复
没看懂,帮顶~

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧