20-2-2011 15:7:11
vì là lấy dữ liệu nên tôi làm 1 bài cho trọn bộ, cách craw cuối cùng
gọi hàm thư viện :
Mã:
using System.Net;
using System.IO;
Đọc hoặc load toàn bộ nội dung HTML web site cần lấy :
Mã:
System.Net.HttpWebRequest myRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://www.kitco.com/pop_windows/exchdetails.html");
myRequest.MaximumAutomaticRedirections = 1;
myRequest.AllowAutoRedirect = true;
System.Net.HttpWebResponse myResponse = (System.Net.HttpWebResponse)myRequest.GetResponse();
Stream resStream = myResponse.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
string readstring = reader.ReadToEnd();
Phân tích nội dung và Regex cần lấy :
Mã:
int tableIndex = readstring.IndexOf("Exchange Rate change from last 24 hours");
readstring = readstring.Substring(tableIndex, readstring.Length - tableIndex);
// xoa mat dong RegexOptions.Compiled trong dong ben duoi
readstring = System.Text.RegularExpressions.Regex.Replace(readstring, @"\n", "");
readstring = System.Text.RegularExpressions.Regex.Match(readstring, @"<table.*?<\/table>").Value;
this.Literal2.Text = readstring;
code demo tên file là Craw3.aspx : DOWNLOAD
http://www.4shared.com/file/mAiQHl9W/khotienvn.htmlXem thêm chi tiết bài viết tại đây :
http://khotien.vn/diendan/default.aspx?g=posts&m=1380