手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#实现通过程序自动抓取远程Web网页信息的代码
C#实现通过程序自动抓取远程Web网页信息的代码
摘要:通过程序自动的读取其它网站网页显示的信息,类似于爬虫程序。比方说我们有一个系统,要提取BaiDu网站上歌曲搜索排名。分析系统在根据得到的数据...

通过程序自动的读取其它网站网页显示的信息,类似于爬虫程序。比方说我们有一个系统,要提取BaiDu网站上歌曲搜索排名。分析系统在根据得到的数据进行数据分析。为业务提供参考数据。

为了完成以上的需求,我们就需要模拟浏览器浏览网页,得到页面的数据在进行分析,最后把分析的结构,即整理好的数据写入数据库。那么我们的思路就是:

1、发送HttpRequest请求。

2、接收HttpResponse返回的结果。得到特定页面的html源文件。

3、取出包含数据的那一部分源码。

4、根据html源码生成HtmlDocument,循环取出数据。

5、写入数据库。

程序如下:

//根据Url地址得到网页的html源码

privatestringGetWebContent(stringUrl)

{

stringstrResult="";

try

{

HttpWebRequestrequest=(HttpWebRequest)WebRequest.Create(Url);

//声明一个HttpWebRequest请求

request.Timeout=30000;

//设置连接超时时间

request.Headers.Set("Pragma","no-cache");

HttpWebResponseresponse=(HttpWebResponse)request.GetResponse();

StreamstreamReceive=response.GetResponseStream();

Encodingencoding=Encoding.GetEncoding("GB2312");

StreamReaderstreamReader=newStreamReader(streamReceive,encoding);

strResult=streamReader.ReadToEnd();

}

catch

{

MessageBox.Show("出错");

}

returnstrResult;

}

为了使用HttpWebRequest和HttpWebResponse,需填名字空间引用

usingSystem.Net;

以下是程序具体实现过程:

privatevoidbutton1_Click(objectsender,EventArgse)

{

//要抓取的URL地址

stringUrl="http://list.mp3.baidu.com/topso/mp3topsong.html?id=1#top2";

//得到指定Url的源码

stringstrWebContent=GetWebContent(Url);

richTextBox1.Text=strWebContent;

//取出和数据有关的那段源码

intiBodyStart=strWebContent.IndexOf("<body",0);

intiStart=strWebContent.IndexOf("歌曲TOP500",iBodyStart);

intiTableStart=strWebContent.IndexOf("<table",iStart);

intiTableEnd=strWebContent.IndexOf("</table>",iTableStart);

stringstrWeb=strWebContent.Substring(iTableStart,iTableEnd-iTableStart+8);

//生成HtmlDocument

WebBrowserwebb=newWebBrowser();

webb.Navigate("about:blank");

HtmlDocumenthtmldoc=webb.Document.OpenNew(true);

htmldoc.Write(strWeb);

HtmlElementCollectionhtmlTR=htmldoc.GetElementsByTagName("TR");

foreach(HtmlElementtrinhtmlTR)

{

stringstrID=tr.GetElementsByTagName("TD")[0].InnerText;

stringstrName=SplitName(tr.GetElementsByTagName("TD")[1].InnerText,"MusicName");

stringstrSinger=SplitName(tr.GetElementsByTagName("TD")[1].InnerText,"Singer");

strID=strID.Replace(".","");

//插入DataTable

AddLine(strID,strName,strSinger,"0");

stringstrID1=tr.GetElementsByTagName("TD")[2].InnerText;

stringstrName1=SplitName(tr.GetElementsByTagName("TD")[3].InnerText,"MusicName");

stringstrSinger1=SplitName(tr.GetElementsByTagName("TD")[3].InnerText,"Singer");

//插入DataTable

strID1=strID1.Replace(".","");

AddLine(strID1,strName1,strSinger1,"0");

stringstrID2=tr.GetElementsByTagName("TD")[4].InnerText;

stringstrName2=SplitName(tr.GetElementsByTagName("TD")[5].InnerText,"MusicName");

stringstrSinger2=SplitName(tr.GetElementsByTagName("TD")[5].InnerText,"Singer");

//插入DataTable

strID2=strID2.Replace(".","");

AddLine(strID2,strName2,strSinger2,"0");

}

//插入数据库

InsertData(dt);

dataGridView1.DataSource=dt.DefaultView;

}

【C#实现通过程序自动抓取远程Web网页信息的代码】相关文章:

C#委托初级使用的实例代码

C#访问应用程序配置文件的方法

用C#实现启动另一程序的方法实例

C#中通过API实现的打印类 实例代码

C#+MO实现一个道路编辑软件(刚开始)

Visual C#.Net 网络程序开发-Socket篇第1/2页

c# 抓取Web网页数据分析

深入C#任务管理器中应用程序选项隐藏程序本身的方法详解

C#实现类似qq的屏幕截图程序

c#根据文件类型获取相关类型图标的方法代码

精品推荐
分类导航