最近一直在寫個抓取天貓數據的小工具,用到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