C语言如何使用爬虫ip代码示例

数据抓取工作必不可少的就是需要海量爬虫ip支持,那么使用爬虫ip怎么配合C语言来运行项目?下列文档的代码可以供大家参考下。

#include 
#include 
#include 
#include "curl/curl.h"
using namespace std;
static size_t WriteMemoryCallback(void* ptr, size_t size, size_t nmemb, void* stream) {
  size_t nsize = size * nmemb;
  string* strdata = (string*)stream;
  if (strdata)
    strdata->append((const char*)ptr, nsize);
  return nsize;
}
void Proxy(string& resp) {
    CURL* curl = curl_easy_init();
    CURLcode res;
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "https://jshk.com.cn/ip");
        curl_easy_setopt(curl, CURLOPT_PROXY, "http://219.151.125.106:31615"); 
        //代理IP地址和端口号
        curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, "895314XY:24D6YB309ZCB"); 
        //代理IP的AuthKey和AuthPwd
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        if (res != CURLE_OK) {
            fprintf(stderr, "request failed: %s\n", curl_easy_strerror(res));
        }
    }
}
int main()
{
    string response;
    Proxy(response);
    cout << response << endl;
        return 0;
}

你可能感兴趣的:(c语言,爬虫,爬虫IP,API提取,数据抓取)