2010-12-30 wcdj

由于平时喜欢网上购物,下面实现一个简单的当当网商品价格查询助手,减少打开网页的操作,可以将自己喜欢的商品记录在favorite.txt文件中,通过对商品价格的查询以决定何时购买。

主要代码如下:

void CTool4UDlg::OnBnClickedBtnPrice() { // select URL from combo control int iPos=((CComboBox*)GetDlgItem(IDC_COMBO_WEBLIST))->GetCurSel();// choose current line, index from zero if (-1 == iPos)// choose none { AfxMessageBox("You should choose one commodity!"); return; } vector<string>::const_iterator citer=strURL.begin(); string strCurURL=*(citer+iPos);// current URL // show information in the Static Text control CStatic* pStatic=(CStatic*)GetDlgItem(IDC_INFO); pStatic->SetWindowText("正在获取数据,请稍后......"); CInternetSession session; CHttpFile *file=NULL; //LPCTSTR pstrURL=_T("http://www.baidu.com"); LPCTSTR pstrURL=_T(strCurURL.c_str()); DWORD_PTR dwContext = 1; DWORD dwFlags = INTERNET_FLAG_TRANSFER_ASCII | INTERNET_OPEN_TYPE_PRECONFIG | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_COOKIES | INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_MAKE_PERSISTENT ; LPCTSTR pstrHeaders = NULL; DWORD dwHeadersLength = 0; CString myData; // OpenURL method can throw exceptions of type CInternetException* try { // see http://msdn.microsoft.com/en-us/library/aa385328%28VS.85%29.aspx session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,32000); session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000); session.SetOption(INTERNET_OPTION_CONNECT_RETRIES,5); file=(CHttpFile *)session.OpenURL(pstrURL,dwContext,dwFlags,pstrHeaders,dwHeadersLength); } catch(CInternetException *e) { TCHAR tszError[1024]; if(e->GetErrorMessage(tszError, sizeof(tszError))) { AfxMessageBox(tszError); } e->Delete(); //AfxMessageBox("Open URL Error!"); // show information in the Static Text control CStatic* pStatic=(CStatic*)GetDlgItem(IDC_INFO); pStatic->SetWindowText("Open URL error, plz check the network connection!"); file=NULL; delete file; return; } CString strPromotion, strOriginal, strSale;// check used CString strProKey, strOrigKey, strSaleKey;// key words if (NULL != file) { //Do something here with the web request DWORD dwRet ; ((CHttpFile *)file)->QueryInfoStatusCode(dwRet); if(dwRet == 200) { //AfxMessageBox("connect"); // show information in the Static Text control CStatic* pStatic=(CStatic*)GetDlgItem(IDC_INFO); pStatic->SetWindowText("已连接上,正在获取数据......"); } // to make file writeable SetFileAttributes("web_data",FILE_ATTRIBUTE_NORMAL); // record web data to a text file CStdioFile fout; LPCSTR pstrSaveFileName="web_data"; UINT nSaveFlag=CFile::modeCreate | CFile::modeWrite | CFile::typeText; if( !fout.Open(pstrSaveFileName, nSaveFlag) ) { file->Close(); file=NULL; delete file; session.Close(); return; } // set web_data to hide SetFileAttributes("web_data",FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN); CString str, strTmp, strMoney; int pos; char szMoney[128]={0}; bool b1, b2, b3; b1=b2=b3=true; while(file->ReadString(str)) { // [1] /* promotions_price_d */ if ((pos=str.Find("promotions_price_d")) != -1) { // char* to CString strTmp.Format("%s", str.GetBuffer(0)+pos); // find money if ((pos=strTmp.Find("¥")) != -1) { char * beg=strTmp.GetBuffer(0)+pos; int i=0, dot_end=0;// use dot_end variable to control loop times while (*beg && dot_end<=3)// ex. 99.00 { // 0x30==0, dot==0x30-2 if (*beg >= 0x30 && *beg <= 0x39 || *beg == (0x30-2) ) { memcpy(szMoney+i,beg,1); ++i; } if (*beg == (0x30-2) || dot_end)// dot { ++dot_end; } ++beg; } // char* to CString strMoney.Format("%s", szMoney); strPromotion=strMoney; } } strTmp.ReleaseBuffer(); str.ReleaseBuffer(); // [2] /* originalPriceTag */ if ((pos=str.Find("市")) != -1 || (pos=str.Find("定")) != -1 && b2) { // char* to CString strTmp.Format("%s", str.GetBuffer(0)+pos); // find money if ((pos=strTmp.Find("¥")) != -1) { char * beg=strTmp.GetBuffer(0)+pos; int i=0, dot_end=0;// use dot_end variable to control loop times while (*beg && dot_end<=3)// ex. 99.00 { // 0x30==0, dot==0x30-2 if (*beg >= 0x30 && *beg <= 0x39 || *beg == (0x30-2) ) { memcpy(szMoney+i,beg,1); ++i; } if (*beg == (0x30-2) || dot_end)// dot { ++dot_end; } ++beg; } // char* to CString strMoney.Format("%s", szMoney); strOriginal=strMoney; b2=false; } } strTmp.ReleaseBuffer(); str.ReleaseBuffer(); // [3] /* salePriceTag */ if ((pos=str.Find("当")) != -1 && b3) { // char* to CString strTmp.Format("%s", str.GetBuffer(0)+pos); // find money if ((pos=strTmp.Find("¥")) != -1) { char * beg=strTmp.GetBuffer(0)+pos; int i=0, dot_end=0;// use dot_end variable to control loop times while (*beg && dot_end<=3)// ex. 99.00 { // 0x30==0, dot==0x30-2 if (*beg >= 0x30 && *beg <= 0x39 || *beg == (0x30-2) ) { memcpy(szMoney+i,beg,1); ++i; } if (*beg == (0x30-2) || dot_end)// dot { ++dot_end; } ++beg; } // char* to CString strMoney.Format("%s", szMoney); strSale=strMoney; b3=false; } } strTmp.ReleaseBuffer(); str.ReleaseBuffer(); str += _T("/n"); fout.WriteString(str); } fout.Close(); //Clean up the file here to avoid a memory leak! file->Close(); file=NULL; delete file; } bool bPro, bOrig, bSale; bPro=bOrig=bSale=true; // check promotions_price_d if (strPromotion.IsEmpty()) { // show the result in the static control CStatic* pStatic=(CStatic*)GetDlgItem(IDC_PROMOTION); pStatic->SetWindowText("Promotion price: NULL"); bPro=false; } else { // show the result in the static control CStatic* pStatic=(CStatic*)GetDlgItem(IDC_PROMOTION); pStatic->SetWindowText("Promotion price: ¥"+strPromotion); } // check originalPriceTag if (strOriginal.IsEmpty()) { // show the result in the static control CStatic* pStatic=(CStatic*)GetDlgItem(IDC_ORIGIANL); pStatic->SetWindowText("Original price: NULL"); bOrig=false; } else { // show the result in the static control CStatic* pStatic=(CStatic*)GetDlgItem(IDC_ORIGIANL); pStatic->SetWindowText("Original price: ¥"+strOriginal); } // check salePriceTag if (strSale.IsEmpty()) { // show the result in the static control CStatic* pStatic=(CStatic*)GetDlgItem(IDC_SALE); pStatic->SetWindowText("DangDang price: NULL"); bSale=false; } else { // show the result in the static control CStatic* pStatic=(CStatic*)GetDlgItem(IDC_SALE); pStatic->SetWindowText("DangDang price: ¥"+strSale); } // calculate discount float fDiscount=0.0, fPro, fOrig, fSale; CString strDiscount; if (bOrig) { // CString to float fOrig=(float)atof(strOriginal.GetBuffer(0)); if (bPro) { fPro=(float)atof(strPromotion.GetBuffer(0)); try { if (fOrig==0) { throw fOrig; } fDiscount=fPro/fOrig*100; // float to CString strDiscount.Format("%.0f",fDiscount); CStatic* pStatic=(CStatic*)GetDlgItem(IDC_DISCOUNT); pStatic->SetWindowText("Discount: "+strDiscount+"折"); } catch (...) { AfxMessageBox("fOrig cannot be zero!"); session.Close(); return; } } else// bSale { fSale=(float)atof(strSale.GetBuffer(0)); try { if (fOrig==0) { throw fOrig; } fDiscount=fSale/fOrig*100; // float to CString strDiscount.Format("%.0f",fDiscount); CStatic* pStatic=(CStatic*)GetDlgItem(IDC_DISCOUNT); pStatic->SetWindowText("Discount: "+strDiscount+"折"); } catch (...) { AfxMessageBox("fOrig cannot be zero!"); session.Close(); return; } } }// end of if (bOrig) else { CStatic* pStatic=(CStatic*)GetDlgItem(IDC_DISCOUNT); pStatic->SetWindowText("Discount: NULL"); } // show information in the Static Text control pStatic=(CStatic*)GetDlgItem(IDC_INFO); pStatic->SetWindowText("当当网 wcdj"); session.Close(); }

实现一个当当网商品价格查询助手相关推荐

  1. 手机版网课查询助手内置答案

    大学网课太难还在四处百度?一软在手解决所有困惑, 哪题不会直接搜哪题,内置多个答案以及查询接口. 地址:点我下载

  2. python 爬取当当网商品价格并写入数据库

    python 爬取数据并写入数据库案例: import requests import pymysql import re from fake_useragent import UserAgent f ...

  3. 当当网书籍数据爬进数据库

    当当网书籍数据爬进数据库 首先我们要考虑到当当网有没有反爬机制,几次后发现是没有的. 一开始我们要考虑我们要爬取的信息是哪些,然后在items.py下面全部写出来. 然后就写爬虫文件 从核心文件开始定 ...

  4. 当当网java项目_基于S2SH框架开发当当网项目

    这是一个当当网的示例源码,本人也是从网上下载下来进行学习的,收获良多,特此分享给各位大牛.采用的是S2SH框架,基本上的功能都实现了.比如注册,登陆,发送邮件等等.项目包里面有项目安装说明,相信各路大 ...

  5. 我的第一个Scrapy 程序 - 爬取当当网信息

    前面已经安装了Scrapy,下面来实现第一个测试程序. 概述 Scrapy是一个爬虫框架,他的基本流程如下所示(下面截图来自互联网) 简单的说,我们需要写一个item文件,定义返回的数据结构:写一个s ...

  6. 一个简单的当当网购物车页面,可以实现简单购物

    点加号可以添加添加商品,点减号可以减商品, 一个简单的当当网页面,可以实现简单的购物效果

  7. 请用Python写一个爬虫程序,爬取当当网图书畅销榜数据

    好的,下面是一个简单的 Python 爬虫代码,可以爬取当当网图书畅销榜数据: import requests from bs4 import BeautifulSoupurl = "htt ...

  8. 转 当当网资深DBA:DB运维四大现代化的实现

    位好,今天我的主题是 <DB运维的四个现代化> ,看标题就能明白,是关于DBA自动化运维平台的事情. http://dbaplus.cn/news-21-855-1.html 主要是分享下 ...

  9. 最后三天!当当网3折购书优惠码限时抢

    感谢这世界的程序员 可以说,没有程序员,就没有现在多姿多彩的互联网. 程序员们通过coding改变世界,创造更加美好的生活.也以实际行动在浮躁的世界里,坚持自己对于知识.技术和创新的追求. 让我们一起 ...

最新文章

  1. 看看现在大型网站都是用什么语言写的 ?
  2. 网络编程学习笔记(辅助数据)
  3. mysql 系统参数优化方法_Mysql 性能优化2 系统参数配置方法 和 文件系统
  4. UA PHYS515A 电磁理论V 电磁波与辐射9 简单辐射系统
  5. 天翼云从业认证(4.9)工业企业上云解决方案
  6. mysql 列 随机数_mysql mmp 某字段插入随机数!(说不定那天就忘记了,存下来再说)...
  7. 二分答案——数列分段 Section II(洛谷 P1182)
  8. PostgreSQL中如何实现密码复杂度检查?
  9. STM8单片机ADC带缓存的连续采样模式
  10. 寻找代理服务器的常用软件
  11. Activiti 工作流表单设计及开发
  12. 面试官的窒息逼问: 到底什么是面向接口编程?
  13. k620显卡 unreal_Modo三维建模系统_搜维尔[SouVR.com]—专业虚拟现实、增强现实、智能智造产品和解决方案超市...
  14. C#今日头条推荐新闻爬取
  15. 真·杂项:资本论阅读笔记(随缘更新)
  16. 实现圣杯布局的三种方式
  17. CSU1256(天朝的单行道)
  18. 大表哥有个项目,100W预算,让我顺手做了算了......
  19. JavaAwt子部件定位设置大小,setBounds(x, y, w, h);setLocation(x, y); setSize(w, h); 一开始不起作用,加个延时起作用了
  20. 算法训练 Beaver's Calculator (蓝桥杯)

热门文章

  1. Golang快速开发框架搭建
  2. HowtoBecomeAhacker[转]
  3. 小样本学习记录————用于深度网络快速适应的模型不可知元学习(MAML)
  4. 通用 漂亮 功能强大的BS架构系统框架
  5. 信道的极限容量——奈氏准则、奈圭斯特公式、信噪比、香农公式
  6. 小觅S系列相机运行VINS-Mono记录
  7. Javascript复制内容到剪切版
  8. centos7安装配置nginx
  9. 百度熊掌号: 2018-2020年, 你不容错过的流量红利!
  10. 在html中如何创建空白子网页,在DreamWeaver中创建空白页