实现一个当当网商品价格查询助手

2010-12-30 wcdj

 

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

 

实现一个当当网商品价格查询助手_第1张图片

 

主要代码如下:

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(); }

 

 

你可能感兴趣的:(实现一个当当网商品价格查询助手)