最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊

簡單使用C語言通過Microhttpd庫采集淘寶

2023-11-02 16:33 作者:華科云商小彭  | 我要投稿


前段時(shí)間給大家分享過不少采集淘寶相關(guān)內(nèi)容的方法,昨天有個(gè)朋友讓我來幫他用C語言編寫一個(gè)采集淘寶的爬蟲程序,并且還要通過Microhttpd庫來實(shí)現(xiàn)。這一點(diǎn)都難不倒我,下面我就將我的代碼示例給大家分享一下,有需要的朋友趕緊來取。

```c

#include

#include

#include

#include

#define HTTP_PORT 8080

struct my_config {

int listen_backlog;

int max_client;

char *document_root;

char *server_name;

};

struct my_server_config {

struct my_config config;

struct MHD_Daemon *daemon;

};

struct my_server_config *my_server_init(void)

{

struct my_server_config *server_config = malloc(sizeof(*server_config));

server_config->config.listen_backlog = 100;

server_config->config.max_client = 10;

server_config->config.document_root = "/home/user/crawler";

server_config->config.server_name = "Crawler";

server_config->daemon = MHD_start_daemon(MHD_USE_HTTPS | MHD_USE_RECURSIVE_PERIODIC "('https://www.duoip.cn/get_proxy:8000')" | MHD_USE_LOCAL_FILE ('./web/index.html'), HTTP_PORT, NULL, my_server_response, NULL, &server_config->config, NULL);

if (server_config->daemon == NULL) {

fprintf(stderr, "Error: Unable to start daemon\n");

exit(EXIT_FAILURE);

}

return server_config;

}

void my_server_free(struct my_server_config *server_config)

{

MHD_stop_daemon(server_config->daemon);

free(server_config->config.document_root);

free(server_config->config.server_name);

free(server_config);

}

static ssize_t my_server_response(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls)

{

ssize_t ret = MHD_NO;

struct my_server_config *server_config = (struct my_server_config *)cls;

if (strcmp(url, "/") == 0) {

char *response = malloc(1024);

snprintf(response, 1024, "Content-Type: text/html\n\nWelcome to the Crawler");

ret = MHD_send_response(connection, MHD_HTTP_OK, strlen(response), response);

free(response);

} else {

/* Redirect to the proxy */

char *proxy_url = malloc(1024);

snprintf(proxy_url, 1024, "http://www.duoip.cn:8000%2F%u", (unsigned int) connection->client_addr.client.sin_port);

ret = MHD_send_response(connection, MHD_HTTP_FOUND, strlen(proxy_url), proxy_url);

free(proxy_url);

}

return ret;

}

int main(int argc, char *argv[])

{

struct my_server_config *server_config = my_server_init();

while (1) {

MHD_run(server_config->daemon);

}

my_server_free(server_config);

return 0;

}

```

這個(gè)程序的工作原理是,首先啟動(dòng)一個(gè)Microhttpd服務(wù)器,并配置它使用HTTPS協(xié)議,完成代理服務(wù)器的配置,以及從本地文件(./web/index.html)提供內(nèi)容。然后,每當(dāng)有客戶端連接到服務(wù)器時(shí),服務(wù)器會(huì)檢查請(qǐng)求的URL。如果URL是"/",那么服務(wù)器會(huì)返回一個(gè)歡迎頁面。否則,服務(wù)器會(huì)創(chuàng)建一個(gè)代理請(qǐng)求,將請(qǐng)求轉(zhuǎn)發(fā)給服務(wù)器,依次循環(huán)工作。


簡單使用C語言通過Microhttpd庫采集淘寶的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國家法律
宕昌县| 浪卡子县| 峨边| 台湾省| 阿坝县| 龙胜| 中西区| 嵩明县| 汕头市| 哈巴河县| 洱源县| 济源市| 顺义区| 荣成市| 宽甸| 延津县| 汉阴县| 伊金霍洛旗| 沁水县| 大悟县| 美姑县| 华池县| 满洲里市| 西充县| 旅游| 长岭县| 恩施市| 会同县| 新津县| 龙陵县| 基隆市| 怀安县| 无极县| 彰化市| 凤山市| 准格尔旗| 和龙市| 上蔡县| 鄱阳县| 武宁县| 贵溪市|