开源项目(库)之libcurl学习(一)
最近抽了点时间,将之前开发中使用到的一些开源库进行了下总结,主要是为了回顾一下自己所使用的一些库基础知识,并且加深理解,在这些库中,首先一个库就是libcurl,这个库很强大,当时在做openstack swift API时使用到了,这个库一个轻量级的HTTP编程库,里面封装了一套基于HTTP的上层应用协议的数据包的基本操作,其支持FTP,FTPS,TFTP,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE和LDAP,跨平台,支持Windows,Unix,Linux等,线程安全,支持Ipv6。并且易于使用。下面就从安装开始,之前的开发是在Windows下开发的,现在的工作环境换为了Linux,但是不要紧,安装过程差不多,步骤如下:
1)下载libcurl文件,下载指令:sudo wget http://curl.haxx.se/download/curl-7.35.0.tar.gz
2)使用指令解压文件:tar -zxvf curl-7.35.0.tar.gz
3)./configure --prefix=/usr/local/libcurl
4)make
5)make install
经过上面几个步骤,libcurl库基本上就安装好了,下面我们就稍微写过小测试程序来测试测试,代码如下:
#ifndef __HTTP_CURL__H
#define __HTTP_CURL__H
#include <boost/smart_ptr.hpp>
#include <boost/xpressive/xpressive_dynamic.hpp>
#include <boost/typeof/typeof.hpp>
#include <curl/curl.h>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
using namespace boost;
using namespace boost::xpressive;
#define MAX_BUFFERSIZE 1024*10
class HttpCurl
{
public:
HttpCurl()
{
conn = NULL;
memset(errBuffer,0,sizeof(errBuffer));
}
~HttpCurl()
{
curl_easy_cleanup(conn);
}
bool HttpCurlInit(string& context)
{
CURLcode code;
string error;
code = curl_global_init(CURL_GLOBAL_DEFAULT);
if(CURLE_OK != code)
{
printf("Failed to global init default\n");
return false;
}
conn = curl_easy_init();
if(NULL == conn)
{
printf("Failed to create CURL\n");
return false;
}
code = curl_easy_setopt(conn,CURLOPT_ERRORBUFFER,error.c_str());
if(CURLE_OK != code)
{
printf("Failed to set error buffer\n");
return false;
}
code = curl_easy_setopt(conn,CURLOPT_WRITEFUNCTION,HttpCurl::write);
if(CURLE_OK != code)
{
printf("Failed to set write\n");
return false;
}
code = curl_easy_setopt(conn,CURLOPT_WRITEDATA,&context);
if(CURLE_OK != code)
{
printf("Failed to set write data\n");
return false;
}
return true;
}
bool setUrl(string& url)
{
CURLcode code;
code = curl_easy_setopt(conn,CURLOPT_URL,url.c_str());
if(CURLE_OK != code)
{
printf("Failed to set URL\n");
return false;
}
return true;
}
bool getHttpResponse()
{
CURLcode code;
std::string error;
code = curl_easy_perform(conn);
if(CURLE_OK != code)
{
printf("Failed to get [%s]",error.c_str());
return false;
}
return true;
}
static long write(void* data,int size,int nmemb,std::string& context)
{
long sizes = size*nmemb;
std::string temp((char*)data,sizes);
context += temp;
return sizes;
}
bool save(const string& context,std::string filename)
{
CURLcode code;
int retcode = 0;
code = curl_easy_getinfo(conn,CURLINFO_RESPONSE_CODE,&retcode);
if((CURLE_OK == code)&& retcode ==200)
{
int length = strlen(context.c_str());
FILE* file = fopen(filename.c_str(),"w+");
fseek(file,0,SEEK_SET);
fwrite(context.c_str(),1,length,file);
fclose(file);
return true;
}
return false;
}
private:
CURL* conn;
char errBuffer[MAX_BUFFERSIZE];
};
测试程序代码如下:
int main()
{
string context;
HttpCurl curl;
curl.HttpCurlInit(context);
curl.setUrl("www.renren.com");
curl.getHttpResponse();
curl.save(context,"text.txt");
return 0;
}
通过简单的测试用例,可以看出libcurl使用的一些基本流程,接下来我们再看看一个使用libcurl写成的单线程爬虫程序,这个程序实现思路很简单,有机会将其改成多线程,代码如下:
class Spider
{
public:
Spider(shared_ptr<HttpCurl>& cul):httpCurl(cul)
{
urlSet.clear();
finishUrlSet.clear();
}
~Spider(){}
bool init(std::string& context)
{
return httpCurl->HttpCurlInit(context);
}
void parseUrl(const string& context)
{
const string tag = "href";
const string tag2 = "\"";
string::size_type tempBegin,tempEnd,iter;
tempBegin = tempEnd = 0;
iter= context.find(tag);
while(iter != string::npos)
{
tempBegin = context.find(tag2,iter);
if(tempBegin != string::npos)
{
++tempBegin;
tempEnd = context.find(tag2,tempBegin);
}
if(tempEnd != string::npos && tempEnd > tempBegin)
{
string url;
url.assign(context,tempBegin,(tempEnd-tempBegin));
urlSet.insert(url);
}
iter = context.find(tag,tempEnd);
}
}
void filterUrl()
{
string tag = "http";
urlSet_Iter iter = urlSet.begin();
for(;iter != urlSet.end();)
{
string::size_type index = (*iter).find(tag);
if(index == string::npos)
urlSet.erase(iter);
iter++;
}
}
bool write(const string& context,const string& filename)
{
return httpCurl->save(context,filename);
}
void start(std::string url,std::string& context)
{
httpCurl->setUrl(url);
httpCurl->getHttpResponse();
parseUrl(context);
filterUrl();
}
void displayUrl()
{
urlSet_Iter iter = urlSet.begin();
for(; iter != urlSet.end();++iter)
{
cout<<*iter<<endl;
}
}
void loop(const std::string& url,std::string& context)
{
start(url,context);
for(urlSet_Iter iter = urlSet.begin();iter != urlSet.end();++iter)
{
if(finishUrlSet.find(*iter) != finishUrlSet.end())
continue;
printf("%s\n",(*iter).c_str());
char filename[64];
memset(filename,0,sizeof(filename));
sprintf(filename,"%d.html",fileIndex++);
context.clear();
start(*iter,context);
write(context,filename);
finishUrlSet.insert(*iter);
}
}
private:
shared_ptr<HttpCurl> httpCurl;
std::set<string> urlSet;
std::set<string> finishUrlSet;
typedef std::set<string>::iterator urlSet_Iter;
};
测试程序:
#include "curlTest.h"
int main()
{
string context;
shared_ptr<HttpCurl> curl(new HttpCurl());
Spider spider(curl);
spider.init(context);
spider.loop("www.renren.com",context);
return 0;
}
测试结果:
http://a.xnimg.cn/favicon-rr.ico?ver=3
http://a.xnimg.cn/n/core/res/certificate.jpg
http://a.xnimg.cn/wap/apple_icon_.png
http://app.renren.com
http://app.renren.com/?origin=40206
http://app.renren.com/activity/specialpage?activity=girlgame&origin=40240
http://app.renren.com/activity/specialpage?activity=newgame&origin=40238
http://app.renren.com/activity/specialpage?activity=sanguo&origin=40239
http://app.renren.com/list?category=10&type=1&origin=40060&menu=1
http://app.renren.com/list?category=10&type=1&origin=40065&menu=1
http://app.renren.com/list?category=10&type=1&origin=40207&menu=1
http://app.renren.com/list?category=11&type=1
http://app.renren.com/list?category=11&type=1&origin=3142&added=0
http://app.renren.com/list?category=11&type=1&origin=40131
http://app.renren.com/list?category=11&type=1&origin=40132
http://app.renren.com/list?category=11&type=1&origin=40189
http://app.renren.com/list?category=11&type=1&origin=50298
http://app.renren.com/list?category=12&type=1&origin=3142&added=0
http://app.renren.com/list?category=12&type=1&origin=40131
http://app.renren.com/list?category=12&type=1&origin=40132
http://app.renren.com/list?category=13&type=1
http://app.renren.com/list?category=13&type=1&origin=3142&added=0
http://app.renren.com/list?category=13&type=1&origin=40131
http://app.renren.com/list?category=13&type=1&origin=40132
http://app.renren.com/list?category=13&type=1&origin=40188
http://app.renren.com/list?category=13&type=1&origin=40189
http://app.renren.com/list?category=13&type=1&origin=50298
http://app.renren.com/list?category=14&type=1
http://app.renren.com/list?category=14&type=1&origin=3113
http://app.renren.com/list?category=14&type=1&origin=3142&added=0
http://app.renren.com/list?category=14&type=1&origin=40188
http://app.renren.com/list?category=14&type=1&origin=50298
http://app.renren.com/list?category=15&type=1&origin=3142&added=0
http://app.renren.com/list?category=15&type=1&origin=40131
http://app.renren.com/list?category=17&type=1&origin=3142&added=0
http://app.renren.com/list?category=19&type=1
http://app.renren.com/list?category=19&type=1&origin=3113
http://app.renren.com/list?category=19&type=1&origin=3142&added=0
http://app.renren.com/list?category=19&type=1&origin=40132
http://app.renren.com/list?category=19&type=1&origin=40188
http://app.renren.com/list?category=19&type=1&origin=50298
http://app.renren.com/list?category=20&type=1&origin=40066&menu=2
http://app.renren.com/list?category=20&type=1&origin=40210&menu=2
^C
总结
本篇博文主要是安装了下libcurl,并且在此基础上实现了一个简单的爬虫程序,代码结构很简单,至于libcurl的一些接口介绍什么的,大家可以去其官网上看,在这里就略过了,在写这个爬虫程序时,一开始一心想用boost里的regex做正则表达式匹配url,但是中间遇到了问题,所以临时自己写了个提取url的parseURL,另外这个爬虫程序使用的是单线程的,有机会的话,我会将其改成支持多线程,其实很多东西在于自己去实践,才能体会到其中的奥秘,好了,这篇博文到此就结束了,多谢。
如果需要,请注明转载,多谢
开源项目(库)之libcurl学习(一)相关推荐
- 【机器人学】机器人开源项目KDL源码学习:(5)KDL如何求解几何雅克比矩阵
这篇文章试图说清楚两件事:1. 几何雅克比矩阵的本质:2. KDL如何求解机械臂的几何雅克比矩阵. 一.几何雅克比矩阵的本质 机械臂的关节空间的速度可以映射到执行器末端在操作空间的速度,这种映射可以通 ...
- C/C++开源项目资源,值得学习。
C/C++开源项目资源~ 值得学习的C语言开源项目 - 1. Webbench Webbench是一个在Linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的UR ...
- 众多Android 开源项目再次推荐,学习不可错过
FBReaderJ FBReaderJ用于Android平台的电子书阅读器,它支持多种电子书籍格式包括:oeb.ePub和fb2.此外还支持直接读取zip.tar和gzip等压缩文档. 项目地址:h ...
- Github优秀Android开源项目,值得引用与学习(图文结合~~~)
刚进来的时候需要加载很多图片和gif图片, 所以想看图片效果需要耐心等待一下. JKeyboardPanelSwitch Android键盘面板冲突 布局闪动处理方案 点我跳转 给大家提供一个底部导航 ...
- Github优秀Android开源项目,值得引用与学习(注意!里面有巨图! )
内容添加(--根据trending(today)/java从上往下添加的,根据时间查看,都是github的项目,以及大佬们的点赞的GitHub项目 ) 刚进来的时候需要加载很多图片和gif图片, 所以 ...
- 【Android】开源图表库MPAndroidChart的学习
android开源图表库MPAndroidChart(中文翻译) MPAndroidChart简化版运行效果: 主要的Api方法: setDescription(String desc) : 设置表格 ...
- 开源机器人库orocos KDL 学习笔记(四):Forward Kinematric
上一篇主要讲述了KDL中运动链的建立方式,以及与其相关的段(Segment)和关节(Joint)的概念,这些是串联机械臂运动学的基础.本篇主要讲述KDL中正运动学解的实现方式及其使用. 1. puma ...
- 10个完整的Android开源项目,值得大家学习借鉴
1.项目:Rocket.Chat Github地址:https://github.com/RocketChat/Rocket.Chat Star:14175 Fork:2952 介绍:开源完整的聊天 ...
- 开源机器人库orocos KDL 学习笔记(五):Inverse Kinematric
上一篇主要讲述KDL中正运动学解的实现方式及其使用.本篇主要讲述KDL中逆运动学解的实现方式及其使用. 1. puma560的逆运动学解 首先还是以puma560作为例子,来看一下如何调用KDL的逆运 ...
- 【机器人学】机器人开源项目KDL源码学习:(4)机械臂逆动力学的牛顿欧拉算法
机械臂的逆动力学问题可以认为是:已知机械臂各个连杆的关节的运动(关节位移.关节速度和关节加速度),求产生这个加速度响应所需要的力/力矩.KDL提供了两个求解逆动力学的求解器,其中一个是牛顿欧拉法, ...
最新文章
- 墨卡托坐标转换成经纬度
- 学习C语言深入解剖笔记之关键字的秘密
- vue中textarea标签自适应高度
- Java客户端操作zookeeper:获取及修改节点中的数据内容代码示例
- pom项目install报错没有自己_SAP财务凭证报错:没有项目种类分配到科目
- SMTP Error: Could not connect to SMTP host
- 腾讯无边界网络 致胜企业安全新战场
- 杨澜给80后女孩子的14个忠告
- 左程云 Java 笔记--图
- 学习人工智能导论(3)
- Google浏览器常用设置
- Parameterize Method(令函数携带参数)
- java ligerui_[Java教程]jQuery LigerUI 使用教程入门篇_星空网
- 怎么用html3秒自动跳网页,HTML页面3秒后自动跳转的三种常见方法
- CSP-S 2022题目与CSP-J 2022题目
- 2017山东单招计算机试题,2017年山东单独招生模拟试题(语文/数学/英语)
- 考公 or 直接找工作,该怎么选?
- win linux双系统 启动菜单,通过EasyBCD制作Windows7和Ubuntu双系统启动菜单
- 学习笔记(10):ArcGIS之数字高程模型(DEM)分析上篇视频课程(GIS思维)-DEM的含义与应用...
- 谷歌浏览器第一次开页面很慢...