利用cURL来获取网页信息-Using cURL to get webpage content
2013-03-17 20:29
381 查看
#include <iostream> #include <fstream> #include <vector> #include <sstream> #include <string> extern "C" { #include <stdio.h> //#include <curl/easy.h> #include <curl/curl.h> } using namespace std; int writer(char* data, size_t size, size_t nmemb, string *buffer) { fprintf(stderr, "Hello, I am a function pointer\n"); int result = 0; if (buffer != NULL) { buffer->append(data, size * nmemb); result = size * nmemb; } return result; } int main(int argc, char** args) { CURL* curl; // That is the connection, see : curl_easy_init CURLcode res; // Not needed, see : curl_easy_cleanup string buffer; // see: CURLOPT_WRITEDATA curl = curl_easy_init(); // Initilize web query if (curl) { // set options for web query curl_easy_setopt(curl, CURLOPT_URL, args[1]); curl_easy_setopt(curl, CURLOPT_HEADER, 0); // No we don't need the header of the web content, Set to 0 and // curl ignores the first line curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);//Don't follow anything else than the particular url requested curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer); // Function Pointer 'writer' manages the required buffer size curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); // Data Pointer &buffer stores downloaded web content } else { fprintf(stderr, "Error 1\n"); return 0; } res = curl_easy_perform(curl); //string s = (res); cout << res << endl; curl_easy_cleanup(curl); std::istringstream iss(buffer); string line, item; int linenum = 0; while (getline(iss, line)) { linenum ++; cout << "\nline #" << linenum << " : " << endl; std::istringstream linestream(line); int itemnum = 0; while (getline(linestream, item, ',')) { itemnum ++; cout << "Item # " << itemnum << " : " << item << endl; } } return 0; }
相关文章推荐
- 利用cURL来获取网页信息-Using cURL to get webpage content
- Infinite Scroll To Load More Content in Webpage Using Ajax, and Jquery
- How to get a web page content type
- Using Timer to get history values from EDNA database, an error is prompted in the page : "Sys.WebForms.PageRequestManagerTimeout
- [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework
- c++利用libcurl获取http网页的 response headers 等信息
- 利用python获取网页信息以及插入Access数据库
- [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework
- 利用WinAPI(GetFileVersionInfo)获取文件版本信息
- How to get the file's icon using C++ - 用C++如何获取文件的图标
- c#利用WebClient和WebRequest获取网页源代码
- 通过getContentResolver,ListView,Adapter获取系统多媒体信息
- 利用curl自动获取并下载网页的方法
- 利用WinAPI(GetFileVersionInfo)获取文件版本信息
- C# HttpWebRequest 绝技 根据URL地址获取网页信息
- HTML5利用WebRTC的getUserMedia获取摄像头信息模拟拍照及视频(完整示例)
- 如何获取网卡相关信息(包括mac, ip, mask, gateway等)?---利用GetAdaptersInfo
- php用get_meta_tags轻松获取网页的meta信息
- Get WEB Page Content over Linux C
- How To Query Performance Monitor Counters Using a Web Page