首页 > 代码库 > C语言调用curl库抓取网页图片(转)

C语言调用curl库抓取网页图片(转)

思路是先用curl抓取网页源码,然后以关键字寻找出图片网址。

 范例:
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <curl/curl.h>
 5  
 6 void get_key_from_str(char *origin, char *str1, char *str2, char *key);
 7  
 8 int main(int argc, char **argv)
 9 {
10     CURL *curl;
11     FILE *fp = NULL, *fp_read = NULL;
12     int file_size = 0;
13     char *tmp = NULL;
14     int flag = 0;
15     char key[1024] = {0};
16     char str1[128] = {0};
17     char str2[128] = {0};
18 
19     if ((fp = fopen("test.txt", "w")) == NULL)
20     {
21         return 1;
22     }
23     curl = curl_easy_init();
24     if (curl)
25     {
26         flag = 1;
27         curl_easy_setopt(curl, CURLOPT_URL, argv[1]);     //curl设置网址
28         curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);     //将网页源码存在文件中,好像只能存文件,不能存字符串
29         curl_easy_perform(curl);//开始抓取
30         curl_easy_cleanup(curl);
31         fclose(fp);
32         fp_read = fopen("test.txt", "rb");
33         fseek(fp_read, 0, SEEK_END);      //把指针移到文本最后
34         file_size = ftell(fp_read)     ;//算出文本大小
35         fseek(fp_read, 0, SEEK_SET);     //再把指针移到最前面
36         tmp = (char *)malloc(file_size * sizeof(char));     //malloc一段内存
37         printf("file_size:%d\n", file_size);
38         fread(tmp, file_size, sizeof(char), fp_read);     //读取文件
39         fclose(fp_read);
40         snprintf(str1, sizeof(str1), "img src=http://www.mamicode.com/"");
41         snprintf(str2, sizeof(str2), "\"");
42         get_key_from_str(tmp, str1, str2, key);     //取得网址
43  
44         printf("key:%s\n", key);
45         free(tmp);
46     }
47  
48     if (!flag)
49     {
50         fclose(fp);
51     }
52  
53     return 0;
54 }
55 //抓取str1和str2之间的关键字
56 void get_key_from_str(char *origin, char *str1, char *str2, char *key)
57 {
58     char *p = strstr(origin, str1);
59     char *q = strstr(p + strlen(str1), str2);
60     int len = q - p - strlen(str1);
61     snprintf(key, len + 1, "%s", p + strlen(str1));
62     key[len + 1] = \0;
63 }


gcc -g -Wall main.c -o test -lcurl

./test url

C语言调用curl库抓取网页图片(转)