获取电驴首页推荐信息和指定栏目信息
标 题: 获取电驴首页推荐信息和指定栏目信息
作 者: itdef
链 接: http://www.cnblogs.com/itdef/p/4081963.html
欢迎转帖 请保持文本完整并注明出处
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; } |
关于字符集转换的 文章
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话