自己动手编写CSDN博客备份工具-blogspider

简介:

来源:http://blog.csdn.net/gzshun

  我之前一直在看lucene,nutch,发现有这么一个现成的小应用,特转来学习下!mark一下。


网络爬虫(又被称为网页蜘蛛,网络机器人),是一种按照一定的规则,自动的抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁,自动索引,模拟程序或者蠕虫。


网络爬虫最重要的任务,就是从互联网搜索出需要的信息,将网页抓取下来并分析,很多搜索引擎,比如百度,谷歌,后台都有一只很强悍的网络爬虫,用来访问互联网上的网页,图片,视频等内容,并建立索引数据库,使用户能在百度搜索引擎中搜索到您网站的网页、图片、视频等内容。

我们常见的几个大型搜索引擎公司的爬虫名称:
1.谷歌(Google) -> Googlebot
2.百度(Baidu)爬虫名称:Baiduspider
3.雅虎(Yahoo) -> Yahoo! Slurp
4.有道(Yodao) -> YodaoBot
5.搜狗(sogou) -> Sogou spider
6.MSN -> msmbot
7.腾讯搜搜 -> Sosospider

最近我突然想自己动手写一只小型的博客爬虫,将自己在CSDN博客网站写的文章给抓取下来,想做个博客备份工具。当了解到网络爬虫的用途后,就来动手实现一个应用,用来备份自己在CSDN的博客,这样即使没有网络,或者文章丢失了,我手头都有一个备份。记得上次在微博看过CSDN创始人蒋涛先生说的一句话,他想做一个CSDN博客生成PDF文档的工具,其实那也相当于对自己博客的备份,这样就能很方便的浏览自己的写的文章。

我写的这个"blogspider"程序,将会把自己博客信息提取出来,并将所有的文章下载到本地。这里只是简单的下载网页而已,里面的图片我没有下载,那得涉及到太多的东西。如果电脑有网络,将会很容易的看到博客里面的图片,如果没有网络,图片将无法显示。

blogspider程序由C语言编写的,基于Linux平台,我编写该程序的环境如下:
[plain]  view plain copy
  1. gzshun@ubuntu:~$ uname -a  
  2. Linux ubuntu 2.6.32-24-generic-pae #39-Ubuntu SMP Wed Jul 28 07:39:26 UTC 2010 i686 GNU/Linux  
  3. gzshun@ubuntu:~$ gcc -v  
  4. Using built-in specs.  
  5. Target: i486-linux-gnu  
  6. Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu  
  7. Thread model: posix  
  8. gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)  

本人在putty终端测试程序,可以正确的显示中文,要设置为UTF-8,或者GB2312,如果显示乱码,切换一下字符集试试。


一.blogspider的功能简介:

1.获取博客的基本信息:
  博客标题
  博客访问量
  博客积分
  博客排名
  博客原创文章数量
  博客转载文章数量
  博客译文文章数量
  博客评论数量

2.下载博客到本地:
  博客主题
  博客发表日期
  博客阅读次数
  博客评论次数

二.blogspider涉及到的知识点:
1.文件I/O
2.网络编程socket
3.数据结构-链表
4.内存分配


三.blogspider程序执行流程:
以我的博客为例:
1.将"http://blog.csdn.net/gzshun"主页下载到本地
2.分析该主页,获取到博客的URL
3.将博客的URL添加到爬虫链表
4.遍历爬虫链表,将博客下载到本地
5.将下载日志保存在gzshun.log


四.blogspider程序的重要部分:

1.爬虫链表的结构体

[cpp]  view plain copy
  1. typedef struct tag_blog_info {  
  2.     char *b_url;           /*网址*/  
  3.     char *b_host;          /*网站服务器主机名*/  
  4.     char *b_page_file;     /*页面文件名称*/  
  5.     char *b_local_file;    /*本地保存的文件名称*/  
  6.     char *b_title;         /*博客主题*/  
  7.     char *b_date;          /*博客发表日期*/  
  8.     int   b_port;          /*网址端口号*/  
  9.     int   b_sockfd;        /*网络套接字*/  
  10.     int   b_reads;         /*阅读次数*/  
  11.     int   b_comments;      /*评论次数*/  
  12.     int   b_download;      /*下载状态*/  
  13.     int   b_lock;          /*处理锁*/  
  14.     int   b_seq_num;       /*序号*/  
  15. }blog_info;  
  16.   
  17. typedef struct tag_blog_spider {  
  18.     blog_info *blog;  
  19.     struct tag_blog_spider *next;  
  20. }blog_spider;  

2.博客基本信息结构体
[cpp]  view plain copy
  1. typedef struct tag_blog_rank {  
  2.     int   b_page_total;    /*博客总页数*/  
  3.     char *b_title;         /*博客标题*/  
  4.     char *b_page_view;     /*博客访问量*/  
  5.     char *b_integral;      /*博客积分*/  
  6.     char *b_ranking;       /*博客排名*/  
  7.     char *b_original;      /*博客原创文章数量*/  
  8.     char *b_reship;        /*博客转载文章数量*/  
  9.     char *b_translation;   /*博客译文文章数量*/  
  10.     char *b_comments;      /*博客评论数量*/  
  11. }blog_rank;  

3.定义的函数
[cpp]  view plain copy
  1. static char *strrstr(const char *s1, const char *s2);  
  2. static char *strfchr(char *s);  
  3. static int  init_spider(blog_spider **spider);  
  4. static int  init_rank(blog_rank **rank);  
  5. static void insert_spider(blog_spider *spider_head, blog_spider *spider);  
  6. static int  spider_size(blog_spider *spider_head);  
  7. static void print_spider(blog_spider *spider_head);  
  8. static void print_rank(blog_rank *rank);  
  9. static void free_spider(blog_spider *spider_head);  
  10. static void free_rank(blog_rank *rank);  
  11. static int get_blog_info(blog_spider *spider_head, blog_rank *rank);  
  12. static int analyse_index(blog_spider *spider_head);  
  13. static int download_index(blog_spider *spider_head);  
  14. static int download_blog(blog_spider *spider);  
  15. static int get_web_host(const char *hostname);  
  16. static int connect_web(const blog_spider *spider);  
  17. static int send_request(const blog_spider * spider);  
  18. static int recv_response(const blog_spider * spider);  

4.strrstr是自己实现的,C库没有提供

1.strrstr函数:从一个字符串中查找指定字符串,返回最后一次出现的地址
程序如下:

[cpp]  view plain copy
  1. /************************************************************** 
  2. strrstr  : 查找指定字符串, 返回最后一次出现的地址, 自己实现 
  3. ***************************************************************/  
  4. static char *strrstr(const char *s1, const char *s2)  
  5. {  
  6.     int len2;  
  7.     char *ps1;  
  8.   
  9.     if (!(len2 = strlen(s2))) {  
  10.         return (char *)s1;  
  11.     }  
  12.       
  13.     ps1 = (char *)s1 + strlen(s1) - 1;  
  14.     ps1 = ps1 - len2 + 1;  
  15.   
  16.     while (ps1 >= s1) {  
  17.         if ((*ps1 == *s2) && (strncmp(ps1, s2, len2) == 0)) {  
  18.             return (char *)ps1;  
  19.         }  
  20.         ps1--;  
  21.     }  
  22.   
  23.     return NULL;  
  24. }  

5.初始化爬虫链表
[cpp]  view plain copy
  1. /********************************************************* 
  2. 初始化博客爬虫的链表节点, 申请空间并赋空值 
  3. *********************************************************/  
  4. static int init_spider(blog_spider * * spider)  
  5. {  
  6.     *spider = (blog_spider *)malloc(sizeof(blog_spider));  
  7.     if (NULL == *spider) {  
  8.         #ifdef SPIDER_DEBUG  
  9.         fprintf(stderr, "malloc: %s\n", strerror(errno));  
  10.         #endif  
  11.         return -1;  
  12.     }  
  13.   
  14.     (*spider)->blog = (blog_info *)malloc(sizeof(blog_info));  
  15.     if (NULL == (*spider)->blog) {  
  16.         #ifdef SPIDER_DEBUG  
  17.         fprintf(stderr, "malloc: %s\n", strerror(errno));  
  18.         #endif  
  19.         free(*spider);  
  20.         return -1;  
  21.     }  
  22.   
  23.     (*spider)->blog->b_url           = NULL;  
  24.     (*spider)->blog->b_host          = strdup(CSDN_BLOG_HOST);  
  25.     (*spider)->blog->b_page_file     = NULL;  
  26.     (*spider)->blog->b_local_file    = NULL;  
  27.     (*spider)->blog->b_title         = NULL;  
  28.     (*spider)->blog->b_date          = NULL;  
  29.     (*spider)->blog->b_port          = CSDN_BLOG_PORT;  
  30.     (*spider)->blog->b_sockfd        = 0;  
  31.     (*spider)->blog->b_reads         = 0;  
  32.     (*spider)->blog->b_comments      = 0;  
  33.     (*spider)->blog->b_download      = BLOG_UNDOWNLOAD;  
  34.     (*spider)->blog->b_lock          = BLOG_UNLOCK;  
  35.     (*spider)->blog->b_seq_num       = 0;  
  36.           
  37.     (*spider)->next = NULL;  
  38.   
  39.     return 0;  
  40. }  

6.初始化博客基本信息结构体
[cpp]  view plain copy
  1. /********************************************************* 
  2. 初始化博客基本信息结构体,包含以下几个变量: 
  3. 1.博客页面总页数 
  4. 2.博客标题 
  5. 3.博客访问量 
  6. 4.博客积分 
  7. 5.博客排名 
  8. 6.博客原创文章数量 
  9. 7.博客转载文章数量 
  10. 8.博客译文文章数量 
  11. 9.博客评论数量 
  12. *********************************************************/  
  13. static int init_rank(blog_rank **rank)  
  14. {  
  15.     *rank = (blog_rank *)malloc(sizeof(blog_rank));  
  16.     if (NULL == *rank) {  
  17.         #ifdef SPIDER_DEBUG  
  18.         fprintf(stderr, "malloc: %s\n", strerror(errno));  
  19.         #endif  
  20.         return -1;  
  21.     }  
  22.   
  23.     (*rank)->b_page_total      = 0;  
  24.     (*rank)->b_title           = NULL;  
  25.     (*rank)->b_page_view       = NULL;  
  26.     (*rank)->b_integral        = NULL;  
  27.     (*rank)->b_ranking         = NULL;  
  28.     (*rank)->b_original        = NULL;  
  29.     (*rank)->b_reship          = NULL;  
  30.     (*rank)->b_translation     = NULL;  
  31.     (*rank)->b_comments        = NULL;  
  32.   
  33.     return 0;  
  34. }  

五.blogspider遇到的问题:
1.博客标题如果有'/','?',或者其他不规则的符号,文件将会创建失败。
  解决方案:将不规则的符号赋空,并在后面连接"xxx"字符串,表示省略;
2.在接受网站服务器响应的时候,要将select函数的时间设置长点,有时候因为网络差的问题,将会超时导致退出程序。在blogspider里面,将timeout设置30s。
3.本程序在考虑加入多线程遍历爬虫链表,经过尝试,连接网站服务器会出现竞争问题,将导致连接延时,影响程序效率,暂时不考虑。

六.blogspider运行截图:




使用blogspider:

这里以下载我的博客为例,我CSDN的ID是:gzshun, 网址是:http://blog.csdn.net/gzshun





title              : 博客标题

url                : 博客网址

date            : 博客发表日期

reads          : 博客阅读次数

comments  : 博客评论次数

download   : 博客下载状态


以下这张图片是在windows查看的,通过samba连接到ubuntu服务器。我博客上面的所有文章已经成功地下载到本地。


打开下载在本地的html文件,此时有网络。

打开下载在本地的html文件,此时无网络。



若需要blogspider的源程序,请留下您的E-mail(注意要写成我后面的那种形式,否则会被非法网络爬虫抓取),或者直接联系我的E-mail:gzshuns#163.com (#->@).


前一篇博文《自己动手编写CSDN博客备份工具-blogspider》介绍了blogspider的使用,使用方法很简单,blogspider可以将自己的CSDN博客下载到本地,这里也只提供最基本的功能。这两天有很多哥们儿给我发邮件,想要blogspider的源码,该程序是开源的,有需要的可以留下联系方式。
今天就介绍下blogspider的源代码,其实这里面比较核心的东西就是如何向网站服务器申请我们需要的网页文件。在Java语言,有提供一些网络包,已经将HTTP协议的东西都集成在了包里面,那实现起来就比较简单。最近由于春运期间,大家都在12306网站购票,于是网上就出现了一款抢票的软件,那是用Java写的,是一个谷歌插件。其实那个软件是我一个同事以前的同事写出来的,我们都从这里受益,也买到了回家过年的票,在这里感谢那位牛人。
向Java程序员了解了一下,那个软件的实现原理很简单,步骤如下:
1.访问网站获取网站信息
2.接受到网站服务器的响应消息
3.根据用户选择(硬座,硬卧)的消息再提交到网站服务器
4.得到网站的结果

主要是2个操作:一个是GET方法,一个是POST方法。
GET方法: 从网站服务器下载网页消息,比如网页浏览器可以浏览CSDN网站的新闻与图片,这些都是从网站服务器GET下载到本地;
POST方法:从本地将资料提交到网站服务器,比如在CSDN博客写完文章要点击发表博客,这时候是将一篇文章的所有信息给POST到CSDN服务器。
blogspider的主要目的,就是下载功能,这里使用的是GET方法,用C语言写的都比较低级,这些最基本的都需要自己来实现,等有空看看面向对象编程语言的实现。

废话少说,源码说话:

一.贴出代码中的调试宏,汗,太儿戏了

[cpp]  view plain copy
  1. /*Debug program macro*/  
  2. #if 0  
  3. #define SPIDER_DEBUG  
  4. #endif  

二.贴出代码中的一些宏定义,这些涉及到HTML文件的语法,但本代码不需要会html,只需要最基本的字符串处理:

[cpp]  view plain copy
  1. #define BUFSIZE          1024  
  2.   
  3. #define HTML_ARTICLE     ("<span class=\"link_title\">")  
  4. #define HTML_MULPAGE     ("class=\"pagelist\"")  
  5. #define BLOG_NEXT_LIST   ("article/list")  
  6. #define BLOG_TITLE       ("title=\"")  
  7. #define BLOG_HREF        ("<a href=\"")  
  8. #define BLOG_DATE        ("<span class=\"link_postdate\">")  
  9. #define BLOG_READ        ("<span class=\"link_view\"")  
  10. #define BLOG_COMMENT     ("<span class=\"link_comments\"")  
  11. #define BLOG_SPAN_HEAD   ("<span>")  
  12. #define BLOG_SPAN_END    ("</span>")  
  13. #define BLOG_RANK        ("blog_rank")  
  14. #define BLOG_LI          ("<li>")  
  15. #define BLOG_INDEX       ("index.html")  
  16. #define CSDN_BLOG_URL    ("http://blog.csdn.net")  
  17. #define CSDN_BLOG_HOST   ("blog.csdn.net")  
  18. #define CSDN_BLOG_PORT   (80)  
  19.   
  20. #define BLOG_LOCK        (10)  
  21. #define BLOG_UNLOCK      (11)  
  22. #define BLOG_DOWNLOAD    (20)  
  23. #define BLOG_UNDOWNLOAD  (21)  

上面的BLOG_LOCK,BLOG_UNLOCK是爬虫链表的处理锁,这是扩展预留的,现在还没用。本来要用多线程来处理链表,但经过测试,会产生竞争,导致connect超时,这等过完年再试试。

三.这里再给出爬虫链表的结构体与博客存放基本信息的结构体,里面有多一些变量,但没真正的使用,有些只是预留而已:

[cpp]  view plain copy
  1. typedef struct tag_blog_info {  
  2.     char *b_url;           /*网址*/  
  3.     char *b_host;          /*网站服务器主机名*/  
  4.     char *b_page_file;     /*页面文件名称*/  
  5.     char *b_local_file;    /*本地保存的文件名称*/  
  6.     char *b_title;         /*博客主题*/  
  7.     char *b_date;          /*博客发表日期*/  
  8.     int   b_port;          /*网址端口号*/  
  9.     int   b_sockfd;        /*网络套接字*/  
  10.     int   b_reads;         /*阅读次数*/  
  11.     int   b_comments;      /*评论次数*/  
  12.     int   b_download;      /*下载状态*/  
  13.     int   b_lock;          /*处理锁*/  
  14.     int   b_seq_num;       /*序号*/  
  15. }blog_info;  
  16.   
  17. typedef struct tag_blog_spider {  
  18.     blog_info *blog;  
  19.     struct tag_blog_spider *next;  
  20. }blog_spider;  
  21.   
  22. typedef struct tag_blog_rank {  
  23.     int   b_page_total;    /*博客总页数*/  
  24.     char *b_title;         /*博客标题*/  
  25.     char *b_page_view;     /*博客访问量*/  
  26.     char *b_integral;      /*博客积分*/  
  27.     char *b_ranking;       /*博客排名*/  
  28.     char *b_original;      /*博客原创文章数量*/  
  29.     char *b_reship;        /*博客转载文章数量*/  
  30.     char *b_translation;   /*博客译文文章数量*/  
  31.     char *b_comments;      /*博客评论数量*/  
  32. }blog_rank;  

四.在一个程序中,使用全局变量不是最好的方法,但都有优缺点:

使用全局变量:
1.优点:操作简单,不用提供太多的函数形参;
2.缺点:不好维护,代码可读性差;所以该程序只使用了3个全局变量。

[cpp]  view plain copy
  1. /*global variable*/  
  2. static int g_seq_num = 0;  
  3. static char csdn_id[255];  
  4. static struct hostent *web_host;  

web_host变量用来保存"blog.csdn.net"主机信息,在初始化socket的使用会使用到里面的IP地址, web_host->h_addr_list[0];

五.程序中定义了很多函数,如下:

[cpp]  view plain copy
  1. static char *strrstr(const char *s1, const char *s2);//从s1字符串中查找s2字符串,返回最后一次出现的地址  
  2. static char *strfchr(char *s);//过滤掉s字符串中不规则的字符  
  3. static int  init_spider(blog_spider **spider);//初始化博客爬虫节点,必须使用指针的指针,否则达不到预期效果  
  4. static int  init_rank(blog_rank **rank);//初始化博客存放基本信息的结构体  
  5. static void insert_spider(blog_spider *spider_head, blog_spider *spider);//将博客插入爬虫链表  
  6. static int  spider_size(blog_spider *spider_head);//计算爬虫链表的长度  
  7. static void print_spider(blog_spider *spider_head);//打印爬虫链表,保存到当前目录的*.log文件  
  8. static void print_rank(blog_rank *rank);//打印博客基本信息  
  9. static void free_spider(blog_spider *spider_head);//释放爬虫链表的空间  
  10. static void free_rank(blog_rank *rank);//释放博客基本信息的空间  
  11. static int get_blog_info(blog_spider *spider_head, blog_rank *rank);//从博客主页获取博客标题,博客文章的总页数,积分,排名等信息  
  12. static int analyse_index(blog_spider *spider_head);分析每一页博客的信息,并添加进爬虫链表  
  13. static int download_index(blog_spider *spider_head);//下载博客主页  
  14. static int download_blog(blog_spider *spider);//下载每一篇博客  
  15. static int get_web_host(const char *hostname);//得到"blog.csdn.net"网站的主机信息  
  16. static int connect_web(const blog_spider *spider);//初始化socket,并连接网站服务器  
  17. static int send_request(const blog_spider * spider);//给网站服务器发送请求  
  18. static int recv_response(const blog_spider * spider);//接受网站服务器的响应消息  

六.先给出上述2个字符串处理函数,这家伙,有点罗嗦
[cpp]  view plain copy
  1. /************************************************************** 
  2. strrstr  : 查找指定字符串, 返回最后一次出现的地址, 自己实现 
  3. ***************************************************************/  
  4. static char *strrstr(const char *s1, const char *s2)  
  5. {  
  6.     int len2;  
  7.     char *ps1;  
  8.   
  9.     if (!(len2 = strlen(s2))) {  
  10.         return (char *)s1;  
  11.     }  
  12.       
  13.     ps1 = (char *)s1 + strlen(s1) - 1;  
  14.     ps1 = ps1 - len2 + 1;  
  15.   
  16.     while (ps1 >= s1) {  
  17.         if ((*ps1 == *s2) && (strncmp(ps1, s2, len2) == 0)) {  
  18.             return (char *)ps1;  
  19.         }  
  20.         ps1--;  
  21.     }  
  22.   
  23.     return NULL;  
  24. }  
  25.   
  26. /********************************************************* 
  27. strfchr  : 查找指定字符串中不规则的字符, 并赋空 
  28. 若没有删除这些不规则的字符,则创建文件的时候将会出错 
  29. *********************************************************/  
  30. static char *strfchr(char *s)  
  31. {  
  32.     char *p = s;  
  33.       
  34.     while (*p) {  
  35.         if (('/' == *p) || ('?' == *p)) {  
  36.             *p = 0;  
  37.             strcat(s, "xxx");  
  38.               
  39.             return p;  
  40.         }  
  41.         p++;  
  42.     }  
  43.       
  44.     return NULL;  
  45. }  

引用星爷的一句话: " 功夫其实绝对是适合男女老幼的,打打杀杀只是大家对它的误解。功夫更加是一种艺术,一种不屈的精神。所以,一直以来我都在找方法想将功夫重新包装起来,使得你们这些升斗小民对功夫能够有更深一层的了解。 ".
轻松一下,继续:

七.初始化爬虫链表,我把很多处理都给独立到函数了,这样可以增加程序的可读性,不能将所有功能都在main函数实现.

[cpp]  view plain copy
  1. /********************************************************* 
  2. 初始化博客爬虫的链表节点, 申请空间并赋空值 
  3. *********************************************************/  
  4. static int init_spider(blog_spider * * spider)  
  5. {  
  6.     *spider = (blog_spider *)malloc(sizeof(blog_spider));  
  7.     if (NULL == *spider) {  
  8.         #ifdef SPIDER_DEBUG  
  9.         fprintf(stderr, "malloc: %s\n", strerror(errno));  
  10.         #endif  
  11.         return -1;  
  12.     }  
  13.   
  14.     (*spider)->blog = (blog_info *)malloc(sizeof(blog_info));  
  15.     if (NULL == (*spider)->blog) {  
  16.         #ifdef SPIDER_DEBUG  
  17.         fprintf(stderr, "malloc: %s\n", strerror(errno));  
  18.         #endif  
  19.         free(*spider);  
  20.         return -1;  
  21.     }  
  22.   
  23.     (*spider)->blog->b_url           = NULL;  
  24.     (*spider)->blog->b_host          = strdup(CSDN_BLOG_HOST);  
  25.     (*spider)->blog->b_page_file     = NULL;  
  26.     (*spider)->blog->b_local_file    = NULL;  
  27.     (*spider)->blog->b_title         = NULL;  
  28.     (*spider)->blog->b_date          = NULL;  
  29.     (*spider)->blog->b_port          = CSDN_BLOG_PORT;  
  30.     (*spider)->blog->b_sockfd        = 0;  
  31.     (*spider)->blog->b_reads         = 0;  
  32.     (*spider)->blog->b_comments      = 0;  
  33.     (*spider)->blog->b_download      = BLOG_UNDOWNLOAD;  
  34.     (*spider)->blog->b_lock          = BLOG_UNLOCK;  
  35.     (*spider)->blog->b_seq_num       = 0;  
  36.           
  37.     (*spider)->next = NULL;  
  38.   
  39.     return 0;  
  40. }  
  41.   
  42. /********************************************************* 
  43. 初始化博客基本信息结构体,包含以下几个变量: 
  44. 1.博客页面总页数 
  45. 2.博客标题 
  46. 3.博客访问量 
  47. 4.博客积分 
  48. 5.博客排名 
  49. 6.博客原创文章数量 
  50. 7.博客转载文章数量 
  51. 8.博客译文文章数量 
  52. 9.博客评论数量 
  53. *********************************************************/  
  54. static int init_rank(blog_rank **rank)  
  55. {  
  56.     *rank = (blog_rank *)malloc(sizeof(blog_rank));  
  57.     if (NULL == *rank) {  
  58.         #ifdef SPIDER_DEBUG  
  59.         fprintf(stderr, "malloc: %s\n", strerror(errno));  
  60.         #endif  
  61.         return -1;  
  62.     }  
  63.   
  64.     (*rank)->b_page_total      = 0;  
  65.     (*rank)->b_title           = NULL;  
  66.     (*rank)->b_page_view       = NULL;  
  67.     (*rank)->b_integral        = NULL;  
  68.     (*rank)->b_ranking         = NULL;  
  69.     (*rank)->b_original        = NULL;  
  70.     (*rank)->b_reship          = NULL;  
  71.     (*rank)->b_translation     = NULL;  
  72.     (*rank)->b_comments        = NULL;  
  73.   
  74.     return 0;  
  75. }  

八.爬虫链表的一些处理,这些都比较简单,就都贴出来吧
[cpp]  view plain copy
  1. /********************************************************* 
  2. 将博客爬虫节点插入爬虫链表 
  3. *********************************************************/  
  4. static void insert_spider(blog_spider * spider_head, blog_spider * spider)  
  5. {  
  6.     blog_spider *pspider;  
  7.   
  8.     pspider = spider_head;  
  9.   
  10.     while (pspider->next) {  
  11.         pspider = pspider->next;  
  12.     }  
  13.       
  14.     pspider->next = spider;  
  15. }  
  16.   
  17. /********************************************************* 
  18. 返回爬虫链表长度 
  19. *********************************************************/  
  20. static int spider_size(blog_spider * spider_head)  
  21. {  
  22.     int count = 0;  
  23.     blog_spider *pspider;  
  24.   
  25.     pspider = spider_head;  
  26.   
  27.     while (pspider->next) {  
  28.         pspider = pspider->next;  
  29.         count++;  
  30.     }  
  31.       
  32.     return count;  
  33. }  

篇幅有点长,待下篇文章...
周星驰:你来这里干什么?
赵薇:我想帮你们比赛。
周星驰:你怎么帮?你快点回火星吧,地球是很危险的。


唐僧:你想要啊?悟空,你要是想要的话你就说话嘛,你不说我怎么知道你想要呢,虽然你很有诚意地看着我,可是你还是要跟我说你想要的。你真的想要吗?那你就拿去吧!你不是真的想要吧?难道你真的想要吗?…… 
悟空:我Kao!


在开篇,先happy下,有个好心情,才能天天向上,奋发图强,自强不息。

继《自己动手编写CSDN博客备份工具-blogspider》与《自己动手编写CSDN博客备份工具-blogspider之源码分析(1)》博文后,继续贴出处理的一些函数,原理很简单。

一.在博客的下载过程中,打印了一些信息到屏幕,也保存到了*.log文件

[cpp]  view plain copy
  1. /********************************************************* 
  2. 将爬虫链表的内容打印到log文件,格式为"csdn_id.log",比如 
  3. 我的博客的地址为: "gzshun.log" 
  4. *********************************************************/  
  5. static void print_spider(blog_spider *spider_head)  
  6. {  
  7.     char file[BUFSIZE] = {0};  
  8.     char tmpbuf[BUFSIZE] = {0};  
  9.     blog_spider *spider;  
  10.     FILE *fp;  
  11.   
  12.     sprintf(file, "%s.log", csdn_id);  
  13.   
  14.     fp = fopen(file, "a+");  
  15.     if (NULL == fp) {  
  16.         #ifdef SPIDER_DEBUG  
  17.         fprintf(stderr, "fopen: %s\n", strerror(errno));  
  18.         #endif  
  19.         return;  
  20.     }  
  21.       
  22.     setvbuf(fp, NULL, _IONBF, 0);  
  23.     fseek(fp, 0, SEEK_END);  
  24.       
  25.     spider = spider_head->next;  
  26.     while (spider) {  
  27.         fprintf(fp, "%d:\n"  
  28.                     "%-15s : %s\n"  
  29.                     "%-15s : %s\n"  
  30.                     "%-15s : %s\n"  
  31.                     "%-15s : %d\n"  
  32.                     "%-15s : %d\n"  
  33.                     "%-15s : %s\n\n",  
  34.                     spider->blog->b_seq_num,  
  35.                     "title", spider->blog->b_title,  
  36.                     "url", spider->blog->b_url,  
  37.                     "date", spider->blog->b_date,  
  38.                     "reads", spider->blog->b_reads,  
  39.                     "comments", spider->blog->b_comments,  
  40.                     "download",   
  41.                     (spider->blog->b_download == BLOG_DOWNLOAD) ? "Download" : "UnDownload");  
  42.   
  43.         spider = spider->next;  
  44.     }  
  45.       
  46.     fclose(fp);  
  47. }  
  48. /********************************************************* 
  49. 将博客的基本信息打印到标准输出 
  50. *********************************************************/  
  51. static void print_rank(blog_rank *rank)  
  52. {  
  53.     char file[BUFSIZE] = {0};  
  54.     FILE *fp;  
  55.   
  56.     sprintf(file, "%s.log", csdn_id);  
  57.   
  58.     fp = fopen(file, "w+");  
  59.     if (NULL == fp) {  
  60.         #ifdef SPIDER_DEBUG  
  61.         fprintf(stderr, "fopen: %s\n", strerror(errno));  
  62.         #endif  
  63.         return;  
  64.     }  
  65.     setvbuf(stdout, NULL, _IONBF, 0);  
  66.       
  67.     fprintf(stdout, "CSDN ID : %s\n"  
  68.                     "TITLE   : %s\n"  
  69.                     "URL     : %s/%s\n"  
  70.                     "%s\n"  
  71.                     "%s\n"  
  72.                     "%s\n"  
  73.                     "%s\n"  
  74.                     "%s\n"  
  75.                     "%s\n"  
  76.                     "%s\n",  
  77.                     csdn_id,  
  78.                     rank->b_title,  
  79.                     CSDN_BLOG_URL,  
  80.                     csdn_id,  
  81.                     rank->b_page_view,  
  82.                     rank->b_integral,  
  83.                     rank->b_ranking,  
  84.                     rank->b_original,  
  85.                     rank->b_reship,  
  86.                     rank->b_translation,  
  87.                     rank->b_comments);  
  88.   
  89.     fprintf(fp, "CSDN ID : %s\n"  
  90.                 "TITLE   : %s\n"  
  91.                 "URL     : %s/%s\n"  
  92.                 "%s\n"  
  93.                 "%s\n"  
  94.                 "%s\n"  
  95.                 "%s\n"  
  96.                 "%s\n"  
  97.                 "%s\n"  
  98.                 "%s\n",  
  99.                 csdn_id,  
  100.                 rank->b_title,  
  101.                 CSDN_BLOG_URL,  
  102.                 csdn_id,  
  103.                 rank->b_page_view,  
  104.                 rank->b_integral,  
  105.                 rank->b_ranking,  
  106.                 rank->b_original,  
  107.                 rank->b_reship,  
  108.                 rank->b_translation,  
  109.                 rank->b_comments);  
  110.   
  111.     fclose(fp);  
  112. }  

唐僧:喂喂喂!大家不要生气,生气会犯了嗔戒的!悟空你也太调皮了,我跟你说过叫你不要乱扔东西,你怎么又…你看我还没说完你又把棍子给扔掉了!月光宝盒是宝物,你把他扔掉会污染花草草也是不对的! 

二.申请了空间,在程序结束后必须释放,要不内存泄露了,污染到内存,污染到花花草草也是不对的.

[cpp]  view plain copy
  1. /********************************************************* 
  2. 释放爬虫链表的空间 
  3. *********************************************************/  
  4. static void free_spider(blog_spider * spider_head)  
  5. {  
  6.     blog_spider *pspider;  
  7.     blog_spider *tmp;  
  8.   
  9.     pspider = spider_head;  
  10.       
  11.     while (pspider) {  
  12.         if (pspider->blog->b_url) {  
  13.             free(pspider->blog->b_url);  
  14.         }  
  15.         if (pspider->blog->b_host) {  
  16.             free(pspider->blog->b_host);  
  17.         }  
  18.         if (pspider->blog->b_page_file) {  
  19.             free(pspider->blog->b_page_file);  
  20.         }  
  21.         if (pspider->blog->b_local_file) {  
  22.             free(pspider->blog->b_local_file);  
  23.         }  
  24.         if (pspider->blog->b_title) {  
  25.             free(pspider->blog->b_title);  
  26.         }  
  27.         if (pspider->blog->b_date) {  
  28.             free(pspider->blog->b_date);  
  29.         }  
  30.           
  31.         free(pspider->blog);  
  32.   
  33.         tmp = pspider->next; /*保存下一个节点地址*/  
  34.         free(pspider);  
  35.         pspider = tmp;  
  36.     }  
  37. }  
  38.   
  39. /********************************************************* 
  40. 释放博客基本信息结构体空间 
  41. *********************************************************/  
  42. static void free_rank(blog_rank *rank)  
  43. {  
  44.     if (rank->b_title) {  
  45.         free(rank->b_title);  
  46.     }  
  47.     if (rank->b_page_view) {  
  48.         free(rank->b_page_view);  
  49.     }  
  50.     if (rank->b_integral) {  
  51.         free(rank->b_integral);  
  52.     }  
  53.     if (rank->b_ranking) {  
  54.         free(rank->b_ranking);  
  55.     }  
  56.     if (rank->b_original) {  
  57.         free(rank->b_original);  
  58.     }  
  59.     if (rank->b_reship) {  
  60.         free(rank->b_reship);  
  61.     }  
  62.     if (rank->b_translation) {  
  63.         free(rank->b_translation);  
  64.     }  
  65.     if (rank->b_comments) {  
  66.         free(rank->b_comments);  
  67.     }  
  68.       
  69.     free(rank);  
  70. }  

三.下载个人博客的主页,并分析出必要的信息,比如下载:http://blog.csdn.net/gzshun主页,程序将该文件保存到了本地的"index.html"文件中,先贴出一点html文件的源码,这样就更加清晰的了解代码的字符串解析:

博客标题:

[html]  view plain copy
  1. <div class="header">  
  2. <div id="blog_title">  
  3.     <h1>  
  4.         <a href="/gzshun">Open Linux C/C++专栏</a></h1>  
  5.     <h2></h2>  
  6.     <div class="clear">  
  7.     </div>  
  8. </div>  

博客的总页数:
[html]  view plain copy
  1. <!--显示分页-->  
  2. <div id="papelist" class="pagelist">  
  3. <span> 35条数据  共2页</span><strong>1</strong> <a href="/gzshun/article/list/2">2</a> <a href="/gzshun/article/list/2">下一页</a> <a href="/gzshun/article/list/2">尾页</a>   
  4. 只需要获取到"尾页"前面的数字即可.  

博客的排名,积分信息:
[html]  view plain copy
  1.   <span>gzshun</span>  
  2. </div>  
  3. <div id="blog_medal">  
  4. </div>  
  5. <ul id="blog_rank">  
  6.   <li>访问:<span>54524次</span></li>  
  7.   <li>积分:<span>1070分</span></li>  
  8.   <li>排名:<span>第5615名</span></li>  
  9. </ul>  
  10. <ul id="blog_statistics">  
  11.   <li>原创:<span>29篇</span></li>  
  12.   <li>转载:<span>6篇</span></li>  
  13.   <li>译文:<span>0篇</span></li>  
  14.   <li>评论:<span>209条</span></li>  
  15. </ul>  

贴出源码,这几个字符串解析函数没必要看,自己看下html的规则就能解析出来了:
[cpp]  view plain copy
  1. /********************************************************** 
  2. 获取博客的基本信息,包括以下几点(以下是按照页面的顺序, 
  3. 若不按照该顺序,每次查找必须重设偏移量到开头rewind(fp)): 
  4. 这里获取很多信息, 具体参考blog_spider与blog_rank结构体 
  5. **********************************************************/  
  6. static int get_blog_info(blog_spider * spider_head, blog_rank * rank)  
  7. {  
  8.     FILE *fp;  
  9.     int count;  
  10.     char *posA, *posB, *posC, *posD, *posE;  
  11.     char tmpbuf[BUFSIZE]   = {0};  
  12.     char tmpbuf2[BUFSIZE]  = {0};  
  13.     char line[BUFSIZE]     = {0};  
  14.     char *rank_info_addr[7];  
  15.       
  16.     fp = fopen(spider_head->blog->b_local_file, "r");  
  17.     if (NULL == fp) {  
  18.         fprintf(stderr, "fopen: %s\n", strerror(errno));  
  19.         return -1;  
  20.     }  
  21.   
  22.     /*查找博客的标题*/  
  23.     sprintf(tmpbuf, "<a href=\"/%s\">", csdn_id);  
  24.     while (fgets(line, sizeof(line), fp)) {  
  25.         posA = strstr(line, tmpbuf);  
  26.           
  27.         if (posA) {  
  28.             posA += strlen(tmpbuf);  
  29.             posB = strstr(posA, "</a>");  
  30.             *posB = 0;  
  31.             /*设置爬虫头结点的标题*/  
  32.             spider_head->blog->b_title = strdup(posA);  
  33.             rank->b_title = strdup(posA);  
  34.               
  35.             #ifdef SPIDER_DEBUG  
  36.             printf("%s\n", spider_head->blog->b_title);  
  37.             #endif  
  38.             break;  
  39.         }  
  40.     }  
  41.   
  42.     /*查找博客文章的总页数*/  
  43.     while (fgets(line, sizeof(line), fp)) {  
  44.         posA = strstr(line, HTML_MULPAGE);  
  45.           
  46.         if (posA) {  
  47.             fgets(line, sizeof(line), fp);  
  48.             posB = strrstr(line, BLOG_HREF);  
  49.   
  50.             /* /gzshun/article/list/N, N就是总页数 */  
  51.             memset(tmpbuf, 0, sizeof(tmpbuf));  
  52.             sprintf(tmpbuf, "/%s/%s/", csdn_id, BLOG_NEXT_LIST);  
  53.             posB += strlen(BLOG_HREF) + strlen(tmpbuf);  
  54.             posC = strchr(posB, '"');  
  55.             *posC = 0;  
  56.             rank->b_page_total = atoi(posB);  
  57.             spider_head->blog->b_seq_num = rank->b_page_total;  
  58.               
  59.             #ifdef SPIDER_DEBUG  
  60.             printf("b_page_total = %d\n", rank->b_page_total);  
  61.             #endif  
  62.               
  63.             break;  
  64.         }  
  65.     }  
  66.   
  67.     /*总共有7条信息: 访问 积分 排名 原创 转载 译文 评论*/  
  68.     while (fgets(line, sizeof(line), fp)) {  
  69.         posA = strstr(line, BLOG_RANK);  
  70.   
  71.         if (posA) {  
  72.             count = 0;  
  73.             while (fgets(line, sizeof(line), fp)) {  
  74.                 posB = strstr(line, BLOG_LI);  
  75.                 if (posB) {  
  76.                     if (7 == count) {  
  77.                         break;  
  78.                     }  
  79.                     posB += strlen(BLOG_LI);  
  80.                     posC = strstr(posB, BLOG_SPAN_HEAD);  
  81.                     posD = posC + strlen(BLOG_SPAN_HEAD);  
  82.                     posE = strstr(posD, BLOG_SPAN_END);  
  83.                     *posC = 0;  
  84.                     *posE = 0;  
  85.                     memset(tmpbuf, 0, sizeof(tmpbuf));  
  86.                     memset(tmpbuf2, 0, sizeof(tmpbuf2));  
  87.                     strcpy(tmpbuf, posB);  
  88.                     strcpy(tmpbuf2, posD);  
  89.                     strcat(tmpbuf, tmpbuf2);  
  90.                     rank_info_addr[count++] = strdup(tmpbuf);  
  91.                 }  
  92.             }  
  93.               
  94.             rank->b_page_view     = rank_info_addr[0];  
  95.             rank->b_integral      = rank_info_addr[1];  
  96.             rank->b_ranking       = rank_info_addr[2];  
  97.             rank->b_original      = rank_info_addr[3];  
  98.             rank->b_reship        = rank_info_addr[4];  
  99.             rank->b_translation   = rank_info_addr[5];  
  100.             rank->b_comments      = rank_info_addr[6];  
  101.               
  102.             break;  
  103.         }  
  104.     }  
  105.   
  106.     fclose(fp);  
  107.     return 0;  
  108. }  

以上使用了rank_info_addr数组,是为了在while (fgets(line, sizeof(line), fp)) 循环里面方便赋值。
博客里面可能有很多页,必须我的博客就有2页,这时候网址是这样:
http://blog.csdn.net/gzshun/article/list/1
http://blog.csdn.net/gzshun/article/list/2

所以循环下载blog.csdn.net对应自己的博客列表就行,网页文件的名称如:/gzshun/article/list/1 把gzshun改为自己的csdn的id就是了。
先来杯咖啡,待下一篇文章,前几天奔波在火车上,辛苦啊,今天及时赶到,马上发表,持之以恒。。


周星驰:剪头发不应该看别人怎么剪就发神经跟流行,要配合啊!你看你的发型,完全不配合你的脸型脸型又不配合身型,身型又和发型完全不搭,而且极度不配合啊!!欢哥!你究竟要怎么样啊? 《算死草》

在开篇,先happy下,新年到,开开心心过好年!
已经写了几篇文章,把代码贡献给有需要的人,这里列出前几篇文章,需要的马上跳转,麻利的。。
自己动手编写CSDN博客备份工具-blogspider

自己动手编写CSDN博客备份工具-blogspider之源码分析(1)

自己动手编写CSDN博客备份工具-blogspider之源码分析(2)

本文是blogspider最重要的部分,开始要下载并分析CSDN博客,把博文的URL分析出来,添加进链表,GO!

一.先下载博客主页到本地的index.html

下载网页到本地的步骤:
建立连接 -> 连接网站服务器 -> 发送请求 -> 接收响应 -> 保存到本地
connect_web -> send_request -> recv_response
源码说话:

[cpp]  view plain copy
  1. /***************************************************************** 
  2. 下载个人的博客主页 
  3. *****************************************************************/  
  4. static int download_index(blog_spider * spider_head)  
  5. {  
  6.     int ret;  
  7.       
  8.     ret = connect_web(spider_head);  
  9.     if (ret < 0) {  
  10.         goto fail_download_index;  
  11.     }  
  12.       
  13.     ret = send_request(spider_head);  
  14.     if (ret < 0) {  
  15.         goto fail_download_index;  
  16.     }  
  17.   
  18.     ret = recv_response(spider_head);  
  19.     if (ret < 0) {  
  20.         goto fail_download_index;  
  21.     }  
  22.       
  23.     close(spider_head->blog->b_sockfd);  
  24.       
  25.     return 0;  
  26.       
  27. fail_download_index:  
  28.     close(spider_head->blog->b_sockfd);  
  29.     return -1;  
  30. }  

 

二.建立连接,并连接网站服务器

先从"blog.csdn.net"主机名获取到IP地址,如下:

[cpp]  view plain copy
  1. /********************************************************** 
  2. 根据主机名获取到主机信息,主要是获取到IP地址. 
  3. **********************************************************/  
  4. static int get_web_host(const char * hostname)  
  5. {  
  6.     /*get host ip*/  
  7.     web_host = gethostbyname(hostname);  
  8.     if (NULL == web_host) {  
  9.         #ifdef SPIDER_DEBUG  
  10.         fprintf(stderr, "gethostbyname: %s\n", strerror(errno));  
  11.         #endif  
  12.         return -1;  
  13.     }  
  14.       
  15.     #ifdef SPIDER_DEBUG  
  16.     printf("IP: %s\n", inet_ntoa(*((struct in_addr *)web_host->h_addr_list[0])));  
  17.     #endif  
  18.   
  19.     return 0;  
  20. }  


开始初始化套接字,连接网站服务器:

[cpp]  view plain copy
  1. /********************************************************** 
  2. 初始化SOCKET,并连接到网站服务器 
  3. **********************************************************/  
  4. static int connect_web(const blog_spider * spider)  
  5. {     
  6.     int ret;  
  7.     struct sockaddr_in server_addr;  
  8.   
  9.     /*init socket*/  
  10.     spider->blog->b_sockfd = socket(AF_INET, SOCK_STREAM, 0);  
  11.     if (spider->blog->b_sockfd < 0) {  
  12.         #ifdef SPIDER_DEBUG  
  13.         fprintf(stderr, "socket: %s\n", strerror(errno));  
  14.         #endif  
  15.         return -1;  
  16.     }  
  17.   
  18.     memset(&server_addr, 0, sizeof(server_addr));  
  19.       
  20.     server_addr.sin_family  = AF_INET;  
  21.     server_addr.sin_port    = htons(spider->blog->b_port);  
  22.     server_addr.sin_addr    = *((struct in_addr *)web_host->h_addr_list[0]);  
  23.   
  24.     ret = connect(spider->blog->b_sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));  
  25.     if (ret < 0) {  
  26.         #ifdef SPIDER_DEBUG  
  27.         fprintf(stderr, "connect: %s\n", strerror(errno));  
  28.         #endif  
  29.         return -1;  
  30.     }  
  31.       
  32.     return 0;  
  33. }  


三.发送请求到网站服务器

HTTP协议里面比较重要的有俩方法:GETPOST
向网站服务器发送请求:
GET %s HTTP/1.1\r\n
Accept: */*\r\n
Accept-Language: zh-cn\r\n
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\n
Host: %s:%d\r\n
Connection: Close\r\n\r\n
GET后面跟的是请求的文件,剩下的是一些基本信息,该协议头的结束标志是一个空行,所以程序可以通过判断"\r\n\r\n"为结束标志。具体HTTP协议可以上网搜索一些资料,这里不做介绍。

源码说话:

[cpp]  view plain copy
  1. /********************************************************** 
  2. 向网站服务器发送请求 
  3. **********************************************************/  
  4. static int send_request(const blog_spider * spider)  
  5. {  
  6.     int ret;  
  7.     char request[BUFSIZE];  
  8.       
  9.     memset(request, 0, sizeof(request));  
  10.     sprintf(request,   
  11.         "GET %s HTTP/1.1\r\n"  
  12.         "Accept: */*\r\n"  
  13.         "Accept-Language: zh-cn\r\n"  
  14.         "User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\n"  
  15.         "Host: %s:%d\r\n"  
  16.         "Connection: Close\r\n"  
  17.         "\r\n", spider->blog->b_page_file, spider->blog->b_host, spider->blog->b_port);  
  18.   
  19.     ret = send(spider->blog->b_sockfd, request, sizeof(request), 0);  
  20.     if (ret < 0) {  
  21.         #ifdef SPIDER_DEBUG  
  22.         fprintf(stderr, "send: %s\n", strerror(errno));  
  23.         #endif  
  24.         return -1;  
  25.     }  
  26.       
  27.     #ifdef SPIDER_DEBUG  
  28.     printf("request:\n%s\n", request);  
  29.     #endif  
  30.   
  31.     return 0;  
  32. }  


周星驰:扫地只不过是我的表面工作,我真正地身份是一位研究僧(生)。《少林足球》
轻松一下,继续。。。

四.接收响应消息

向网站服务器发送了请求,当然必须在本地开始接收。由于可能是网速慢的原因,接收响应消息与消息正体速度有点慢。这里使用了select函数与FD_SET集合来处理,当监听到socket可读,才开始读取消息并保存到本地。

[cpp]  view plain copy
  1. /*************************************************************************************** 
  2. 接受网站服务器的反馈信息,得到请求的文件内容 
  3. 向服务器发送请求信息或者服务器的响应消息,以空行结束,所以可以用"\r\n\r\n"来判断结束标志 
  4. select: 
  5. int select (int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval * timeout); 
  6. >0: 正确 
  7. -1: 出错 
  8. 0 : 超时 
  9. void FD_ZERO(fd_set *fdset); // clear all bits in fdset 
  10. void FD_SET(int fd, fd_set *fdset); // turn on the bit for fd in fdset 
  11. void FD_CLR(int fd, fd_set *fdset); // turn off the bit for fd in fdset 
  12. int  FD_ISSET(int fd, fd_set *fdset); // is the bit for fd on in fdset 
  13. ***************************************************************************************/  
  14. static int recv_response(const blog_spider * spider)  
  15. {  
  16.     int ret, end, recvsize, count;  
  17.     char recvbuf[BUFSIZE];  
  18.     fd_set read_fds;  
  19.     struct timeval timeout;  
  20.     FILE *fp;  
  21.   
  22.     /*建议时间要长点, select失败可能的原因是收到网站的响应消息超时*/  
  23.     timeout.tv_sec  = 30;  
  24.     timeout.tv_usec = 0;  
  25.       
  26.     while (1) {  
  27.         FD_ZERO(&read_fds);  
  28.         FD_SET(spider->blog->b_sockfd, &read_fds);  
  29.           
  30.         ret = select(spider->blog->b_sockfd+1, &read_fds, NULL, NULL, &timeout);  
  31.         if (-1 == ret) {  
  32.             /*出错,直接返回错误*/  
  33.             #ifdef SPIDER_DEBUG  
  34.             fprintf(stderr, "select: %s\n", strerror(errno));  
  35.             #endif  
  36.             return -1;  
  37.         }  
  38.         else if (0 == ret) {  
  39.             /*超时, 继续轮询*/  
  40.             #ifdef SPIDER_DEBUG  
  41.             fprintf(stderr, "select timeout: %s\n", spider->blog->b_title);  
  42.             #endif  
  43.             goto fail_recv_response;  
  44.         }  
  45.           
  46.         /*接受到数据*/  
  47.         if (FD_ISSET(spider->blog->b_sockfd, &read_fds)) {  
  48.             end = 0;  
  49.             count = 0;  
  50.   
  51.             /*这里出错可能是文件名不规则,比如"3/5",'/'在Linux是代表目录*/  
  52.             fp = fopen(spider->blog->b_local_file, "w+");  
  53.             if (NULL == fp) {  
  54.                 goto fail_recv_response;  
  55.             }  
  56.   
  57.             spider->blog->b_download = BLOG_DOWNLOAD;  
  58.               
  59.             while (read(spider->blog->b_sockfd, recvbuf, 1) == 1) {  
  60.                 if(end< 4) {  
  61.                     if(recvbuf[0] == '\r' || recvbuf[0] == '\n')  {  
  62.                         end++;  
  63.                     }  
  64.                     else {  
  65.                         end = 0;  
  66.                     }  
  67.                     /*这里是http服务器反馈的消息头,若需要,则可以保存下来*/  
  68.                 }  
  69.                 else {  
  70.                     fputc(recvbuf[0], fp);  
  71.                     count++;  
  72.                     if (1024 == count) {  
  73.                         fflush(fp);  
  74.                     }  
  75.                 }  
  76.             }  
  77.               
  78.             fclose(fp);           
  79.             break;  
  80.         }  
  81.     }  
  82.       
  83.     return 0;  
  84.       
  85. fail_recv_response:  
  86.     spider->blog->b_download = BLOG_UNDOWNLOAD;  
  87.     return -1;  
  88. }  


五.获取CSDN博客的URL,与博客的发表日期,阅读次数,评论次数,并添加进爬虫链表

[cpp]  view plain copy
  1. /***************************************************************** 
  2. 分析个人的博客主页, 获取所有文章的URL, 将博客信息添加到爬虫链表中. 
  3. *****************************************************************/  
  4. static int analyse_index(blog_spider *spider_head)  
  5. {  
  6.     FILE *fp;  
  7.     int ret;  
  8.     int len;  
  9.     int reads, comments;  
  10.     char *posA, *posB, *posC, *posD;  
  11.     char line[BUFSIZE*4]     = {0};  
  12.     char tmpbuf[BUFSIZE]     = {0};  
  13.     char tmpbuf2[BUFSIZE]    = {0};  
  14.     char page_file[BUFSIZE]  = {0};  
  15.     char url[BUFSIZE]        = {0};  
  16.     char title[BUFSIZE]      = {0};  
  17.     char date[BUFSIZE]       = {0};  
  18.   
  19.     fp = fopen(spider_head->blog->b_local_file, "r");  
  20.     if (fp == NULL) {  
  21.         #ifdef SPIDER_DEBUG  
  22.         fprintf(stderr, "fopen: %s\n", strerror(errno));  
  23.         #endif  
  24.         return -1;  
  25.     }  
  26.       
  27.     while (1) {  
  28.         if (feof(fp)) {  
  29.             break;  
  30.         }  
  31.   
  32.         /*查找博客*/  
  33.         while (fgets(line, sizeof(line), fp)) {  
  34.             posA = strstr(line, HTML_ARTICLE);  
  35.   
  36.             if (posA) {  
  37.                 /*查找博客网址*/  
  38.                 posA += strlen(HTML_ARTICLE) + strlen(BLOG_HREF);  
  39.                 posB = strchr(posA, '"');  
  40.                 *posB = 0;  
  41.                 memset(page_file, 0, sizeof(page_file));  
  42.                 memset(url, 0, sizeof(url));  
  43.                 strcpy(page_file, posA);  
  44.                 sprintf(url, "%s%s", CSDN_BLOG_URL, posA);  
  45.   
  46.                 /*查找博客标题*/  
  47.                 posB += 1;  
  48.                 posC = strstr(posB, BLOG_TITLE);  
  49.                 /*与博客地址处在同一行*/  
  50.                 posC += strlen(BLOG_TITLE);  
  51.                 posD = strstr(posC, "\">");  
  52.                 *posD = 0;  
  53.                 memset(title, 0, sizeof(title));  
  54.                 strcpy(title, posC);  
  55.   
  56.                 /*查找博客发表日期*/  
  57.                 while (fgets(line, sizeof(line), fp)) {  
  58.                     posA = strstr(line, BLOG_DATE);  
  59.                       
  60.                     if (posA) {  
  61.                         posA += strlen(BLOG_DATE);  
  62.                         posB = strstr(posA, BLOG_SPAN_END);  
  63.                         *posB = 0;  
  64.                         memset(date, 0, sizeof(date));  
  65.                         strcpy(date, posA);  
  66.                           
  67.                         break;  
  68.                     }  
  69.                 }  
  70.   
  71.                 /*查找博客阅读次数*/  
  72.                 while (fgets(line, sizeof(line), fp)) {  
  73.                     posA = strstr(line, BLOG_READ);  
  74.   
  75.                     if (posA) {  
  76.                         posA += strlen(BLOG_READ);  
  77.                         posB = strchr(posA, '(') + 1;  
  78.                         posC = strchr(posB, ')');  
  79.                         *posC = 0;  
  80.                         reads = atoi(posB);  
  81.                         break;  
  82.                     }  
  83.                 }  
  84.   
  85.                 /*查找博客评论次数*/  
  86.                 while (fgets(line, sizeof(line), fp)) {  
  87.                     posA = strstr(line, BLOG_COMMENT);  
  88.   
  89.                     if (posA) {  
  90.                         posA += strlen(BLOG_COMMENT);  
  91.                         posB = strchr(posA, '(') + 1;  
  92.                         posC = strchr(posB, ')');  
  93.                         *posC = 0;  
  94.                         comments = atoi(posB);  
  95.                         break;  
  96.                     }  
  97.                 }  
  98.   
  99.                 spider_head->blog->b_download = BLOG_DOWNLOAD;  
  100.   
  101.                 blog_spider *spider;  
  102.                 ret = init_spider(&spider);  
  103.                 if (ret < 0) {  
  104.                     return -1;  
  105.                 }  
  106.                   
  107.                 spider->blog->b_page_file   = strdup(page_file);  
  108.                 spider->blog->b_url         = strdup(url);  
  109.                 spider->blog->b_date        = strdup(date);  
  110.                 spider->blog->b_reads       = reads;  
  111.                 spider->blog->b_comments    = comments;  
  112.                 spider->blog->b_seq_num     = ++g_seq_num;  
  113.   
  114.                 memset(tmpbuf, 0, sizeof(tmpbuf));  
  115.                 sprintf(tmpbuf, "%d.%s", spider->blog->b_seq_num, title);  
  116.                 spider->blog->b_title = strdup(tmpbuf);  
  117.   
  118.                 memset(tmpbuf, 0, sizeof(tmpbuf));  
  119.                 memset(tmpbuf2, 0, sizeof(tmpbuf2));  
  120.                 strcpy(tmpbuf2, spider->blog->b_title);  
  121.                 strfchr(tmpbuf2);  
  122.                 sprintf(tmpbuf, "%s/%s.html", csdn_id, tmpbuf2);  
  123.                 spider->blog->b_local_file  = strdup(tmpbuf);  
  124.   
  125.                 /*将博客插入博客爬虫链表*/  
  126.                 insert_spider(spider_head, spider);  
  127.                 fputc('.', stdout);  
  128.             }  
  129.         }  
  130.     }  
  131.   
  132.     fclose(fp);  
  133.       
  134.     #ifdef SPIDER_DEBUG  
  135.     printf("\nspider size = %d\n", spider_size(spider_head));  
  136.     #endif  
  137.       
  138.     return 0;  
  139. }  


代码本身已经注释得很清楚了,看注释就够了。HTTP协议涉及到很多知识点,有空可以写写程序来练练手,blogspider效率上还是不够高,有空添加线程处理,同时下载多个博客,这样才能提高效率。

目录
相关文章
|
1月前
编程笔记 01工具及参考资料
编程笔记 01工具及参考资料
|
5月前
|
数据可视化 项目管理 C++
|
9月前
|
存储 数据库 数据安全/隐私保护
我拿回属于自己的数据,怎么了?|将印象笔记导入笔记软件Notion
我拿回属于自己的数据,怎么了?|将印象笔记导入笔记软件Notion
|
SQL 数据库
实战:第二章:关于EZDML工具使用踩的坑
实战:第二章:关于EZDML工具使用踩的坑
137 0
|
传感器
时隔这么长时间,我把常用的功能整理好了,再来感受VueUse工具库的优雅吧~
时隔这么长时间,我把常用的功能整理好了,再来感受VueUse工具库的优雅吧~
时隔这么长时间,我把常用的功能整理好了,再来感受VueUse工具库的优雅吧~
|
开发者
快速导出印象笔记中的文章至其他平台
快速导出印象笔记中的文章至其他平台
快速导出印象笔记中的文章至其他平台
|
开发工具 git
Git版本控制工具的学习使用笔记
Git安装、 Git常用操作命令、Git连接github、Git查看分支、Git提交新仓库(历史提交记录)
78 0
|
消息中间件 缓存 Kafka
【随笔】学习记录、实用脚本
文章目录 工作随笔 一、kafka 1.1 基础命令 二、es 1.1 基础命令 三、实用小脚本 3.1 判断文件中是否有某个对应的值
88 0
【随笔】学习记录、实用脚本
|
JavaScript 前端开发 C++
前端基础知识备忘(1)
巩固下备忘下前端的基础知识
1490 51

热门文章

最新文章