c++获取电驴首页推荐 示例代码
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
/******************************************************************************* * @file * @author def< qq group: 324164944 > * @blog http://www.cnblogs.com/itdef/ * @brief /*******************************************************************************/ #include "stdafx.h" #include <afxinet.h> #include <atlsimpstr.h> #include <fstream> #include <iostream> #include <sstream> #include <set> using namespace std; #ifdef _DEBUG #define new DEBUG_NEW #endif int GetHttpFileData(CString strUrl,char* DownloadHtmFileName); int ParseHomePageDownloadFile(char* szfileName); int UTF8Str2GBK(const string& strUTF8,string& strGBK); void GetHomePageRecommend(char* szName,const string& strGbk); // 唯一的应用程序对象 CWinApp theApp; using namespace std; int ParseUpdateFile(char* szfileName) { int iRet = -1; if(NULL == szfileName) return iRet; fstream fs(szfileName); stringstream ss ; // 创建字符串流对象 ss << fs.rdbuf(); // 把文件流中的字符输入到字符串流中 fs.close(); string str = ss.str(); // 获取流中的字符串 string strGbk; int i = UTF8Str2GBK(str,strGbk); if(strGbk.size() == 0 || i != 0) { cerr << "transfer utf8 to gbk error" << endl; return iRet; } basic_string <char>::size_type keyWordStart = strGbk.find("<title>"); basic_string <char>::size_type keyWordEnd = strGbk.find("</title>",keyWordStart+1); if( (keyWordStart != string::npos) && (keyWordEnd != string::npos) && (keyWordEnd > keyWordStart) ) { string strKeyWord = strGbk.substr(keyWordStart+7,keyWordEnd - keyWordStart -7); cout << strKeyWord << endl; } keyWordStart = strGbk.find("<div class=\"cv-title\">"); keyWordEnd = strGbk.find("</div>",keyWordStart+1); if( (keyWordStart != string::npos) && (keyWordEnd != string::npos) && (keyWordEnd > keyWordStart) ) { string strKeyWord = strGbk.substr(keyWordStart+22,keyWordEnd - keyWordStart -22); cout << strKeyWord << endl; } iRet = 0; return iRet; } void ShowUpdateInfo(char* szHtmAddress) { if ( 0 != GetHttpFileData(szHtmAddress,"HtmDownloadFile")) { cerr << "GetHttpFileData error once" << endl; } if( 0 != ParseUpdateFile("HtmDownloadFile")) { cerr << "ParseUpdateFile error once" << endl; } } void ShowHomePageElement(char* szHomePageAddress) { if ( 0 != GetHttpFileData(szHomePageAddress,"HtmDownloadFile")) { cerr << "GetHttpFileData error once" << endl; } if( 0 != ParseHomePageDownloadFile("HtmDownloadFile")) { cerr << "GetHttpFileData error once" << endl; } } int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // 初始化 MFC 并在失败时显示错误 if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: 更改错误代码以符合您的需要 _tprintf(_T("错误: MFC 初始化失败\n")); nRetCode = 1; } else { // TODO: 在此处为应用程序的行为编写代码。 ShowHomePageElement("http://www.verycd.com/"); cout << "****************************************************" << endl; ShowUpdateInfo("http://www.verycd.com/entries/790244/"); cout << "****************************************************" << endl; ShowUpdateInfo("http://www.verycd.com/entries/519062/"); cout << "****************************************************" << endl; ShowUpdateInfo("http://www.verycd.com/entries/780306/"); cout << "****************************************************" << endl; ShowUpdateInfo("http://www.verycd.com/entries/522227/"); cout << "****************************************************" << endl; ShowUpdateInfo("http://www.verycd.com/entries/507338/"); cout << "****************************************************" << endl; ShowUpdateInfo("http://www.verycd.com/entries/515005/"); cout << "****************************************************" << endl; ShowUpdateInfo("http://www.verycd.com/entries/794197/"); cout << "****************************************************" << endl; ShowUpdateInfo("http://www.verycd.com/entries/511135/"); cout << "****************************************************" << endl; } system("pause"); return nRetCode; } int UTF8Str2GBK(const string& strUTF8,string& strGBK) { int i = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0); WCHAR *wsz = NULL; TCHAR *tsz = NULL; int iRet = -1; wsz = new WCHAR[i+1]; if( NULL == wsz) { goto UTF8Str2GBK_EXIT; } MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, wsz, i); i = WideCharToMultiByte(CP_ACP, 0, wsz, -1, NULL, 0, NULL, NULL); tsz = new TCHAR[i+1]; if( NULL == tsz) { goto UTF8Str2GBK_EXIT; } WideCharToMultiByte(CP_ACP, 0, wsz, -1, tsz, i, NULL, NULL); strGBK = string(tsz); iRet = 0; UTF8Str2GBK_EXIT: delete []wsz; delete []tsz; return iRet; } int ParseHomePageDownloadFile(char* szfileName) { int iRet = -1; if(NULL == szfileName) return iRet; fstream fs(szfileName); stringstream ss ; // 创建字符串流对象 ss << fs.rdbuf(); // 把文件流中的字符输入到字符串流中 fs.close(); string str = ss.str(); // 获取流中的字符串 string strGbk; int i = UTF8Str2GBK(str,strGbk); if(strGbk.size() == 0 || i != 0) { cerr << "transfer utf8 to gbk error" << endl; return iRet; } cout << "首页大推" << endl; GetHomePageRecommend("VeryCD.TrackEvent('base','首页大推',",strGbk); cout << "首页小推" << endl; GetHomePageRecommend("VeryCD.TrackEvent('base','首页小推',",strGbk); iRet = 0; return iRet; } void GetHomePageRecommend(char* szName,const string& strGbk) { set<string> setKeyWord; //cout << strGbk; basic_string <char>::size_type keyWordStart = strGbk.find(szName); basic_string <char>::size_type keyWordEnd = strGbk.find("')",keyWordStart+1); if( (keyWordStart != string::npos) && (keyWordEnd != string::npos) && (keyWordEnd > keyWordStart + 37) ) { string strKeyWord = strGbk.substr(keyWordStart+37,keyWordEnd - keyWordStart - 37); setKeyWord.insert(strKeyWord); //cout << "电驴首页小推 " << strKeyWord << endl; } while( keyWordStart != string::npos && keyWordEnd != string::npos) { keyWordStart = strGbk.find(szName,keyWordEnd+1); keyWordEnd = strGbk.find("')",keyWordStart+1); if( (keyWordStart != string::npos) && (keyWordEnd != string::npos) && (keyWordEnd > keyWordStart + 37) ) { string strKeyWord = strGbk.substr(keyWordStart+37,keyWordEnd - keyWordStart - 37); setKeyWord.insert(strKeyWord); //cout << "电驴首页小推 " << strKeyWord << endl; } } set<string>::iterator pos; for(pos = setKeyWord.begin();pos != setKeyWord.end();++ pos) { cout << "电驴首页推荐 " << *pos << endl; } } int GetHttpFileData(CString strUrl,char* szDownloadHtmFileName) { CInternetSession Session("Internet Explorer", 0); CHttpFile *pHttpFile = NULL; CString strData; CString strClip; int iRet = -1; if(szDownloadHtmFileName == NULL) { cerr << "DownloadHtmFileName is NULL" << endl; Session.Close(); return iRet; } ofstream of(szDownloadHtmFileName); if (of.bad()) { cerr << "of create file error" << endl; Session.Close(); return iRet; } try { pHttpFile = (CHttpFile*)Session.OpenURL(strUrl); while ( pHttpFile->ReadString(strClip) ) { of << strClip; } }catch(CInternetException* pEx) { TCHAR pszError[64]; pEx->GetErrorMessage(pszError, 64); cerr << __FUNCTION__ << pszError << endl; goto GetHttpFileData_EXIT; } iRet = 0; GetHttpFileData_EXIT: Session.Close(); of.close(); return iRet; } |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=1131