#include <iostream> #include <string> #include <curl/curl.h> size_t write_callback(char *buffer, size_t size, size_t nmemb, std::string &out) { out.append((char*)buffer, size * nmemb); return size * nmemb; } int main() { CURL *curl; CURLcode res; std::string readBuffer; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "http://example.com"); // 替换为实际网址 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); res = curl_easy_perform(curl); curl_easy_cleanup(curl); } else { std::cerr << "Failed to initialize curl" << std::endl; return 1; } curl_global_cleanup(); return 0; }
这段代码需要libcurl库的支持,如果您的系统上没有安装这个库,您需要先安装它,您还需要将"http://example.com"
替换为您想要抓取的实际网址。
还没有评论,来说两句吧...