使用C#爬小说

最近因朋友需要在研究如何从网站上爬小说,说到爬,很多人首先想到的是Python,但是因为没有用过Python,加上时程比较紧,就直接使用C#。

其原理也很简单,就是利用HttpWebRequest对象从网站获取HTML数据包再解析

 HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
 httpReq.Method = "GET";
 httpReq.ContentType = "text/html;charset=utf-8";

 HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse(); HttpWebRequest htt
View Code

实际操作过程中发现有些问题,特意记录下

1、返回的HTML数据包是乱码,这个问题有两种解法,首先是要确保StreamReader的编码格式与网站URL的一致,如下

respStreamReader = new StreamReader(respStream, Encoding.UTF8);

另外就是要看服务器传回的流是否使用了gzip方法压缩,如果用了gzip方法压缩,则要用解压才行

string header = httpResp.GetResponseHeader("Content-Encoding");

StreamReader respStreamReader;
if (header == "gzip")
{
    respStreamReader = new StreamReader(new GZipStream(respStream, CompressionMode.Decompress), Encoding.UTF8);
}

猜你喜欢

转载自www.cnblogs.com/dimg/p/10384936.html
今日推荐