有时候我们需要抓取网页的源代码,并对其进行分析,获取需要的信息,代码如下:
public void getHtml()
...{
WebClient wc = new WebClient();
Stream stream;
string str;
try
...{
//以百度为例
stream = wc.OpenRead("http://www.baidu.com");
StreamReader sr = new StreamReader(stream, System.Text.Encoding.GetEncoding("GB2312"));
//str用来存放html代码
str = sr.ReadToEnd();
}
catch
...{ }
}
注:必须引用名称空间using System.Net;
本文介绍了一种使用C#抓取网页源代码的方法,通过WebClient类从指定URL(如百度首页)获取HTML内容并将其转换为字符串形式。
133

被折叠的 条评论
为什么被折叠?



