通过Url抓取网页内容

近来想学习一下网页抓取技术,监于之前没有这方面的基础,都只是在socke方面的编程,对http方面了解很少,现在到个较好的入门例子,共享学习一下,如果大家以前看过的话,就当是复习吧。还希望高手可以指导一下如何学习这方面的内容,给点指引。

using  System;
using  System.Text;
using  System.Web;
using  System.IO;
using  System.Net;

public   string  ReadUrlContent( string  rUrl) 
{

//  used to build entire input
StringBuilder sb   =   new  StringBuilder();

// 用于作为读取内容操作的缓冲区
byte []        buf  =   new   byte [ 8192 ];

//  请求该页面
HttpWebRequest  request   =  (HttpWebRequest)
 WebRequest.Create(rUrl);

//  获取返回的数据(通过相应)
HttpWebResponse response  =  (HttpWebResponse)
 request.GetResponse();

// 将读取到的数据放入到流里面
Stream resStream  =  response.GetResponseStream();

string  tempString  =   null ;
int  totalcount  =   0 ;
int     count       =   0 ;
FileStream fs 
=  File.Create(Server.MapPath( " urltext.html " ));


do
{
 
// 读取部分的数据
 count  =  resStream.Read(buf,  0 , buf.Length);

 
// 确定读取的数据不为空
  if  (count  !=   0 )
 {
  
// 转换内容格式byte 到 ascii
  tempString  =  Encoding.ASCII.GetString(buf,  0 , count);
  fs.Write(buf,
0 ,count); // 写入文件
  
// 加入到字符串
  sb.Append(tempString);
 }

 totalcount 
+=  count;
}
while  (count  >   0 );

resStream.Close();
fs.Close();

return  sb.ToString();

}

通过这些内容,初步了解了如何通过Url获取单个网页的内容,可以在这些内容的基础上进行分析,获取更多的url,这些就是后话了。
代码地址: http://www.mending.cn/ibook/doc.aspx?uid=17&cid=884&did=6215

转载于:https://www.cnblogs.com/kevin-wu/archive/2007/09/20/899786.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值