利用C#爬取网页HTML数据

转载自:https://blog.csdn.net/liiukangkang/article/details/83689196

//方法一
using System.Text.RegularExpressions;
 
public static void webClientMethod1()
        {
            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            //以字符串的形式返回数据
            string html = wc.DownloadString("https://www.baidu.com/");
 
            //以正则表达式的形式匹配到字符串网页中想要的数据
            MatchCollection matches = Regex.Matches(html, "<a.*>(.*)</a>");
            //依次取得匹配到的数据
            foreach (Match item in matches)
            {
                Console.WriteLine(item.Groups[1].Value);
            }
            Console.ReadKey();
        }
 
 
//方法二
 public static string SendRequest()
        {
            string url = "https://www.baidu.com/";
            Uri httpURL = new Uri(url);
 
            ///HttpWebRequest类继承于WebRequest,并没有自己的构造函数,需通过WebRequest的Creat方法 建立,并进行强制的类型转换   
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
            //httpReq.Headers.Add("cityen", "tj");
 
            ///通过HttpWebRequest的GetResponse()方法建立HttpWebResponse,强制类型转换   
            HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
 
            ///GetResponseStream()方法获取HTTP响应的数据流,并尝试取得URL中所指定的网页内容   
            ///若成功取得网页的内容,则以System.IO.Stream形式返回,若失败则产生ProtoclViolationException错 误。
            System.IO.Stream respStream = httpResp.GetResponseStream();
 
            ///返回的内容是Stream形式的,所以可以利用StreamReader类获取GetResponseStream的内容
            System.IO.StreamReader respStreamReader = new System.IO.StreamReader(respStream, Encoding.UTF8);
            //从流的当前位置读取到结尾
            string  strBuff = respStreamReader.ReadToEnd();
 
            //简单写法,跟上面的结果一样
            //using (var sr = new System.IO.StreamReader(httpReq.GetResponse().GetResponseStream()))
            //{
            //    var result = sr.ReadToEnd();
            //    Console.WriteLine("微信--" + DateTime.Now.ToString() + "--" + result);
            //}
            respStreamReader.Close();
            respStream.Close();
            return strBuff;
        }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值