VC实现访问网站,读取信息
今天更新 吸血鬼日记,为了最早发现人人影视的更新,而且不用麻烦地老是去刷网页,就想用C语言来做一个小程序来实现它。
需要用到libcurl库。思路是访问网页,把网页源码读到数组中,扫描该数组是否有需要的信息。
libcurl安装方法:http://www.2cto.com/kf/201006/51492.html
libcurl相关学习资料:http://bbs.chinaunix.net/thread-2070202-1-1.html
如果遇到报错“无法定位序数55于zlib1.dll”,可用的zlib1.dll下载地址:http://www.xiadll.com/info/zlib1.dll.html
读取到网页乱码的原因:http://linhs.blog.51cto.com/370259/124765
网页乱码解决方法http://www.iteye.com/topic/604821
最后得到我的代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <windows.h> 5 #include <iostream> 6 using namespace std; 7 8 #include <curl/curl.h> 9 10 #pragma comment(lib, "libcurl.lib") 11 //#pragma comment(lib, "libcurl_imp.lib") 12 13 struct MemoryStruct { 14 char *memory; 15 size_t size; 16 }; 17 18 19 static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) 20 { 21 size_t realsize = size * nmemb; 22 struct MemoryStruct *mem = (struct MemoryStruct *)userp; 23 24 mem->memory = (char*)realloc(mem->memory, mem->size + realsize + 1); 25 if (mem->memory == NULL) { 26 /* out of memory! */ 27 printf("not enough memory (realloc returned NULL)\n"); 28 exit(EXIT_FAILURE); 29 } 30 31 memcpy(&(mem->memory[mem->size]), contents, realsize); 32 mem->size += realsize; 33 mem->memory[mem->size] = 0; 34 35 36 return realsize; 37 } 38 39 40 int transfer(char * utf8str) 41 { 42 //UTF8转GBK 43 int len_wchart = MultiByteToWideChar(CP_UTF8,0,utf8str,-1,NULL,0); 44 wchar_t * unicode_2 = new wchar_t[len_wchart] ; 45 MultiByteToWideChar(CP_UTF8,0,utf8str,-1,unicode_2,len_wchart); 46 int len_gbk = WideCharToMultiByte(CP_ACP,0,unicode_2,-1,NULL,0,NULL,NULL); 47 char * gbkstr = new char[len_gbk]; 48 WideCharToMultiByte(CP_ACP,0,unicode_2,-1,gbkstr,len_gbk,NULL,NULL); 49 50 // cout<< "-------- UTF8 => GBK --------" <<endl; 51 // cout<<"GBK字符串长度:"<<strlen(gbkstr)<<endl<<"GBK串:"<<gbkstr<<endl; 52 53 strcpy(utf8str,gbkstr); 54 return 1; 55 } 56 57 58 59 int main(void) 60 { 61 CURL *curl_handle; 62 63 struct MemoryStruct chunk; 64 char *tmp; 65 int i,j; 66 char *object="S04E15"; 67 // FILE *fp; 68 69 chunk.memory =(char*)malloc(1); /* will be grown as needed by the realloc above */ 70 chunk.size = 0; /* no data at this point */ 71 72 curl_global_init(CURL_GLOBAL_ALL); 73 74 /* init the curl session */ 75 curl_handle = curl_easy_init(); 76 77 /* specify URL to get */ 78 curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.yyets.com/php/resource/10985"); 79 80 /* send all data to this function */ 81 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); 82 83 /* we pass our 'chunk' struct to the callback function */ 84 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); 85 86 /* some servers don't like requests that are made without a user-agent 87 field, so we provide one */ 88 // curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); 89 90 91 92 /* get it! */ 93 curl_easy_perform(curl_handle); 94 95 /* cleanup curl stuff */ 96 curl_easy_cleanup(curl_handle); 97 98 /* 99 * Now, our chunk.memory points to a memory block that is chunk.size 100 * bytes big and contains the remote file. 101 * 102 * Do something nice with it! 103 * 104 * You should be aware of the fact that at this point we might have an 105 * allocated data block, and nothing has yet deallocated that data. So when 106 * you're done with it, you should free() it as a nice application. 107 */ 108 109 // printf("%lu bytes retrieved\n", (long)chunk.size); 110 111 transfer(chunk.memory); 112 113 /* 114 * This part of code can store the source code of the webpage to file 115 * Mainly used for debug 116 */ 117 /* 118 if ((fp=fopen("webpage.txt","w")) == NULL) 119 { 120 fprintf(stderr, "Cannot open output file.\n"); 121 return 1; 122 } 123 // fwrite(chunk.memory,chunk.size,1,fp); 124 fprintf(fp,"%s",chunk.memory); 125 126 fclose(fp); 127 */ 128 129 130 /* 131 * Now, we are hunting for the desired resourses 132 * Have fun with it! 133 */ 134 135 for(i=0;i<chunk.size;i++) 136 { 137 for(j=0;j<strlen(object);j++) 138 { 139 if(*(chunk.memory+i+j)!=*(object+j)) 140 break; 141 } 142 if(j==strlen(object)) 143 { 144 printf("FIND %s !!!\n",object); 145 break; 146 } 147 } 148 149 if(i==chunk.size) 150 { 151 printf("Failed in finding it...\n"); 152 } 153 154 if(chunk.memory) 155 free(chunk.memory); 156 157 /* we're done with libcurl, so clean it up */ 158 curl_global_cleanup(); 159 160 return 0; 161 }