C++ 爬虫代理示例

阅读模式

代码示例说明

  1. 代码样例不能直接运行,请替换成您自己的代理信息。
  2. 在不同编程语言的代码示例中,需注意其环境版本。
  3. 示例代码使用遇到问题请联系,我们会为您提供技术支持。

libcurl

1
#include <string>
2
#include <curl/curl.h>
3
#include <iostream>
4
5
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
6
{
7
((std::string*)userp)->append((char*)contents, size * nmemb);
8
return size * nmemb;
9
}
10
int main(void)
11
{
12
13
CURL* curl;
14
CURLcode res;
15
std::string readBuffer;
16
curl = curl_easy_init();
17
if (curl) {
18
19
curl_easy_setopt(curl, CURLOPT_URL, "https://httpbin.org/ip");
20
curl_easy_setopt(curl, CURLOPT_PROXY, "http://t.16yun.cn:31111");
21
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, "username:password");
22
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
23
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
24
/* Perform the request, res will get the return code */
25
res = curl_easy_perform(curl);
26
if(res != CURLE_OK){
27
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
28
}
29
30
/* always cleanup */
31
curl_easy_cleanup(curl);
32
33
std::cout << readBuffer << std::endl;
34
}
35
return 0;
36
}