当前位置: 移动技术网 > IT编程>开发语言>C/C++ > c++使用libcurl

c++使用libcurl

2018年03月04日  | 移动技术网IT编程  | 我要评论

新概念英语视频教程,正规手机充值卡代理,沈暮蝉

最近一直在写个抓取天猫数据的小工具,用到libcurl库

#include 
#include "curl/curl.h"
using namespace std;

void tcurl();
size_t myWrite( void *ptr, size_t size, size_t nmemb, void *stream);

int main(int argc, char const *argv[])
{
    tcurl();
    return 0;
}

void tcurl(){
    cout << "start" << endl;
    char *version = curl_version();
    CURL *curl;
    curl = curl_easy_init();
    string url="",res="";
    url = "http://book.weibo.com";
    cout << version;

    //抓取http数据
    curl_easy_setopt(curl,CURLOPT_URL,url.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&res);
    curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, myWrite);
    curl_easy_setopt(curl, CURLOPT_HEADER, 0);
    //超时,不接受其他信号
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
    //执行
    curl_easy_perform(curl);
    //关闭
    curl_easy_cleanup(curl);
    //cout << version << endl;
    cout << res << endl;
}
//数据的获取
size_t myWrite( void *ptr, size_t size, size_t nmemb, void *stream){
    string *str = dynamic_cast((string*)stream);
    if( 0 == size ||   NULL == ptr){
        return -1;
    }

    char* pData = (char*)ptr;
    str->append(pData,nmemb*size);
    return nmemb*size;
}

g++ tcurl.cpp -lcurl 运行即可

参考

libcurl的使用


libcurl的函数说明

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网