您的位置:首页 > 其它

利用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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: