protected void Button1_Click(object sender, EventArgs e)
...{
string lcUrl = "http://localhost:2344/Web/index.aspx";
HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
// *** Set properties
loHttp.Timeout = 10000; // 10 secs
// loHttp.UserAgent = "Code Sample Web Client";
// *** Retrieve request info headers
HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
Encoding enc = Encoding.GetEncoding("UTF-8"); // Windows default Code Page
StreamReader loResponseStream = new StreamReader(loWebResponse.GetResponseStream(), enc);
string lcHtml = loResponseStream.ReadToEnd();
loWebResponse.Close();
loResponseStream.Close();
Response.Write(lcHtml);
//
string path = "E:/lauka/NewSkyhu/Web/index.html";
Create_html(path, lcHtml);
}
private void Create_html(string allfilename, string htmlcode)
...{
FileStream CreateFile = new FileStream(allfilename, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(CreateFile);
sw.WriteLine(htmlcode);//将拼好的Html代码写入页面中
sw.Close();
}
本文介绍了一种使用C#进行网页抓取的方法,包括设置HTTP请求、获取响应、读取并处理HTML内容,最后将抓取到的数据保存为本地文件。
1142

被折叠的 条评论
为什么被折叠?



