c++使用libcurl
程序员文章站
2022-05-23 08:21:40
最近一直在写个抓取天猫数据的小工具,用到libcurl库
#include
#include "curl/curl.h"
using namespa...
最近一直在写个抓取天猫数据的小工具,用到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的使用
http://blog.csdn.net/huyiyang2010/article/details/7664201
http://blog.chinaunix.net/uid-20692625-id-3203258.html
libcurl的函数说明
http://blog.csdn.net/w13770269691/article/details/8847800
下一篇: 用C语言打印一个菱形