网络爬虫——抓取时的几个小细节

  这一篇咱们聊聊在页面抓取时应该注意到的几个问题。html

  一:网页更新web

  咱们知道,通常网页中的信息是不断翻新的,这也要求咱们按期的去抓这些新信息,可是这个“按期”该怎么理解,也就是多长时间须要浏览器

  抓一次该页面,其实这个按期也就是页面缓存时间,在页面的缓存时间内咱们再次抓取该网页是没有必要的,反而给人家服务器形成压力。缓存

  就好比说我要抓取博客园首页,首先清空页面缓存,性能优化

  


  从Last-Modified到Expires,咱们能够看到,博客园的缓存时间是2分钟,并且我还能看到当前的服务器时间Date,若是我再次服务器

  刷新页面的话,这里的Date将会变成下图中 If-Modified-Since,而后发送给服务器,判断浏览器的缓存有没有过时?cookie

  


  最后服务器发现If-Modified-Since = Last-Modifined的时间,服务器也就返回304了,不过发现这cookie信息真是贼多啊。。。工具

  


  在实际开发中,若是在知道网站缓存策略的状况下,咱们可让爬虫2min爬一次就行了,固然这些都是能够由数据团队来配置维护了,性能

  好了,下面咱们用爬虫模拟一下。优化

  

  1 using System;

  2 using System.Net;

  3

  4 namespace ConsoleApplication2

  5 {

  6 public class Program

  7 {

  8 static void Main(string[] args)

  9 {

  10 DateTime prevDateTime = DateTime.MinValue;

  11

  12 for (int i = 0; i 10; i++)

  13 {

  14 try

  15 {

  16 var url = http://cnblogs.com;

  17

  18 var request = (HttpWebRequest)HttpWebRequest.Create(url);

  19

  20 request.Method = Head;

  21

  22 if (i 0)

  23 {

  24 request.IfModifiedSince = prevDateTime;

  25 }

  26

  27 request.Timeout = 3000;

  28

  29 var response = (HttpWebResponse)request.GetResponse();

  30

  31 var code = response.StatusCode;

  32

  33 //若是服务器返回状态是200,则认为网页已更新,记得当时的服务器时间

  34 if (code == HttpStatusCode.OK)

  35 {

  36 prevDateTime = Convert.ToDateTime(response.Headers[HttpResponseHeader.Date]);

  37 }

  38

  39 Console.WriteLine(当前服务器的状态码:{0}, code);

  40 }

  41 catch (WebException ex)

  42 {

  43 if (ex.Response != null)

  44 {

  45 var code = (ex.Response as HttpWebResponse).StatusCode;

  46

  47 Console.WriteLine(当前服务器的状态码:{0}, code);

  48 }

  49 }

  50 }

  51 }

  52 }

  53 }


  


  二:网页编码的问题

  有时候咱们已经抓取到网页了,准备去解析的时候,tmd的所有是乱码,真是操蛋,好比下面这样,

  


  或许咱们依稀的记得在html的meta中有一个叫作charset的属性,里面记录的就是编码方式,还有一个要点就是

  response.CharacterSet这个属性中一样也记录了编码方式,下面咱们再来试试看。

  


  艹,竟然仍是乱码,蛋疼了,此次须要到官网上面去看一看,到底http头信息里面都交互了些什么,凭什么浏览器能正常显示,

  爬虫爬过来的就不行。

  


  查看了http头信息,终于咱们知道了,浏览器说我能够解析gzip,deflate,sdch这三种压缩方式,服务器发送的是gzip压缩,到这里

  咱们也应该知道了经常使用的web性能优化。

  

  1 using System;

  2 using System.Collections.Generic;

  3 using System.Linq;

  4 using System.Text;

  5 using System.Threading;

  6 using HtmlAgilityPack;

  7 using System.Text.RegularExpressions;

  8 using System.Net;

  9 using System.IO;

  10 using System.IO.Compression;

  11

  12 namespace ConsoleApplication2

  13 {

  14 public class Program

  15 {

  16 static void Main(string[] args)

  17 {

  18 //var currentUrl = http://www.mm5mm.com/;

  19

  20 var currentUrl = http://www.sohu.com/;

  21

  22 var request = WebRequest.Create(currentUrl) as HttpWebRequest;

  23

  24 var response = request.GetResponse() as HttpWebResponse;

  25

  26 var encode = string.Empty;

  27

  28 if (response.CharacterSet == ISO-8859-1)

  29 encode = gb2312;

  30 else

  31 encode = response.CharacterSet;

  32

  33 Stream stream;

  34

  35 if (response.ContentEncoding.ToLower() == gzip)

  36 {

  37 stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);

  38 }

  39 else

  40 {

  41 stream = response.GetResponseStream();

  42 }

  43

  44 var sr = new StreamReader(stream, Encoding.GetEncoding(encode));

  45

  46 var html = sr.ReadToEnd();

  47 }

  48 }

  49 }


  


  三:网页解析

  既然通过千辛万苦拿到了网页,下一个就要解析了,固然正则匹配是个好方法,毕竟工做量仍是比较大的,可能业界也比较推崇

  HtmlAgilityPack这个解析工具,可以将Html解析成XML,而后能够用XPath去提取指定的内容,大大提升了开发速度,性能也

  不赖,毕竟Agility也就是敏捷的意思,关于XPath的内容,你们看懂W3CSchool的这两张图就OK了。

  


  

  1 using System;

  2 using System.Collections.Generic;

  3 using System.Linq;

  4 using System.Text;

  5 using System.Threading;

  6 using HtmlAgilityPack;

  7 using System.Text.RegularExpressions;

  8 using System.Net;

  9 using System.IO;

  10 using System.IO.Compression;

  11

  12 namespace ConsoleApplication2

  13 {

  14 public class Program

  15 {

  16 static void Main(string[] args)

  17 {

  18 //var currentUrl = http://www.mm5mm.com/;

  19

  20 var currentUrl = http://www.sohu.com/;

  21

  22 var request = WebRequest.Create(currentUrl) as HttpWebRequest;

  23

  24 var response = request.GetResponse() as HttpWebResponse;

  25

  26 var encode = string.Empty;

  27

  28 if (response.CharacterSet == ISO-8859-1)

  29 encode = gb2312;

  30 else

  31 encode = response.CharacterSet;

  32

  33 Stream stream;

  34

  35 if (response.ContentEncoding.ToLower() == gzip)

  36 {37 stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);38 }39 else

  40 {

  41 stream = response.GetResponseStream();

  42 }

  43

  44 var sr = new StreamReader(stream, Encoding.GetEncoding(encode));

  45

  46 var html = sr.ReadToEnd();

  47

  48 sr.Close();

  49

  50 HtmlDocument document = new HtmlDocument();

  51

  52 document.LoadHtml(html);

  53

  54 //提取title

  55 var title = document.DocumentNode.SelectSingleNode(//title).InnerText;

  56

  57 //提取keywords

  58 var keywords = document.DocumentNode.SelectSingleNode(//meta[@name='Keywords']).Attributes[content].Value;

  59 }

  60 }

  61 }



  

相关文章
相关标签/搜索