欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c# 抓取Web網(wǎng)頁數(shù)據(jù)分析

 更新時間:2008年11月26日 12:36:14   作者:  
通過程序自動的讀取其它網(wǎng)站網(wǎng)頁顯示的信息,類似于爬蟲程序。比方說我們有一個系統(tǒng),要提取BaiDu網(wǎng)站上歌曲搜索排名。分析系統(tǒng)在根據(jù)得到的數(shù)據(jù)進(jìn)行數(shù)據(jù)分析。為業(yè)務(wù)提供參考數(shù)據(jù)。
為了完成以上的需求,我們就需要模擬瀏覽器瀏覽網(wǎng)頁,得到頁面的數(shù)據(jù)在進(jìn)行分析,最后把分析的結(jié)構(gòu),即整理好的數(shù)據(jù)寫入數(shù)據(jù)庫。那么我們的思路就是:
  1、發(fā)送HttpRequest請求。
  2、接收HttpResponse返回的結(jié)果。得到特定頁面的html源文件。
  3、取出包含數(shù)據(jù)的那一部分源碼。
  4、根據(jù)html源碼生成HtmlDocument,循環(huán)取出數(shù)據(jù)。
  5、寫入數(shù)據(jù)庫。
程序如下:  
復(fù)制代碼 代碼如下:

//根據(jù)Url地址得到網(wǎng)頁的html源碼
private string GetWebContent(string Url)
{
string strResult="";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
    //聲明一個HttpWebRequest請求
request.Timeout = 30000;
//設(shè)置連接超時時間
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);
strResult = streamReader.ReadToEnd();
}
catch
{
MessageBox.Show("出錯");
}
return strResult;
}
為了使用HttpWebRequest和HttpWebResponse,需填名字空間引用
  using System.Net;
以下是程序具體實現(xiàn)過程:
private void button1_Click(object sender, EventArgs e)
{
//要抓取的URL地址
string Url = "http://list.mp3.baidu.com/topso/mp3topsong.html?id=1#top2";
//得到指定Url的源碼
   string strWebContent = GetWebContent(Url);
richTextBox1.Text = strWebContent;
   //取出和數(shù)據(jù)有關(guān)的那段源碼
int iBodyStart = strWebContent.IndexOf("<body", 0);
int iStart = strWebContent.IndexOf("歌曲TOP500", iBodyStart);
int iTableStart = strWebContent.IndexOf("<table", iStart);
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");
foreach (HtmlElement tr in htmlTR)
{
string strID = tr.GetElementsByTagName("TD")[0].InnerText;
string strName = SplitName(tr.GetElementsByTagName("TD")[1].InnerText, "MusicName");
string strSinger = SplitName(tr.GetElementsByTagName("TD")[1].InnerText, "Singer");
strID = strID.Replace(".", "");
//插入DataTable
AddLine(strID, strName, strSinger,"0");
string strID1 = tr.GetElementsByTagName("TD")[2].InnerText;
string strName1 = SplitName(tr.GetElementsByTagName("TD")[3].InnerText, "MusicName");
string strSinger1 = SplitName(tr.GetElementsByTagName("TD")[3].InnerText, "Singer");
//插入DataTable
strID1 = strID1.Replace(".", "");
AddLine(strID1, strName1, strSinger1,"0");
string strID2 = tr.GetElementsByTagName("TD")[4].InnerText;
string strName2 = SplitName(tr.GetElementsByTagName("TD")[5].InnerText, "MusicName");
string strSinger2 = SplitName(tr.GetElementsByTagName("TD")[5].InnerText, "Singer");
//插入DataTable
strID2 = strID2.Replace(".", "");
AddLine(strID2, strName2, strSinger2,"0");
}
//插入數(shù)據(jù)庫
InsertData(dt);
   
dataGridView1.DataSource = dt.DefaultView;
}

相關(guān)文章

最新評論