應用C++法式獲得新浪行情數據的辦法。本站提示廣大學習愛好者:(應用C++法式獲得新浪行情數據的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是應用C++法式獲得新浪行情數據的辦法正文
在平常開辟中我們常常會應用到行情數據,許多的時刻我們依據一個基准數據區結構行情,然則跟著時光的推移然來結構的數據與真實施情數據之間的差距愈來愈年夜。
本問以AG1309為例子來講明,若何應用C++法式來獲得新浪行情數據。(解釋假如合約過時獲得的數據將未空,此時請改換合約信息)。
好了,在這裡就不再將空話,直接給出源碼供年夜家進修!
// HttpDataTest.cpp : 界說掌握台運用法式的進口點。
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#define MAXSIZE 1024
#pragma comment(lib, "Wininet.lib")
void urlopen(_TCHAR*);
std::string GetSubBtFind(char* lpsz);
int Token(const char* pSep, char* pStr, std::vector& refvec);
int _tmain(int argc, _TCHAR* argv[])
{
urlopen(_T("http://hq.sinajs.cn/list=AG1309"));
system("pause");
return 0;
}
void urlopen(_TCHAR* url)
{
HINTERNET hSession = InternetOpen(_T("UrlTest"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(hSession != NULL)
{
HINTERNET hHttp = InternetOpenUrl(hSession, url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
if (hHttp != NULL)
{
wprintf_s(_T("%s\n"), url);
char Temp[MAXSIZE];
ULONG Number = 1;
while (Number > 0)
{
InternetReadFile(hHttp, Temp, MAXSIZE - 1, &Number);
Temp[Number] = '\0';
std::string str= GetSubBtFind(Temp);
;
char szpstr[1024];
strcpy(szpstr, str.c_str());
std::vector vec;
Token(",", szpstr, vec);
printf("%s\n", Temp);
for(int i=0; i
{
printf("%s\n", vec[i]);
}
}
InternetCloseHandle(hHttp);
hHttp = NULL;
}
InternetCloseHandle(hSession);
hSession = NULL;
}
}
std::string GetSubBtFind(char* lpsz)
{
std::string scrStr=lpsz;
std::string str_temp="";
int len=strlen(lpsz);
int beginPos=scrStr.find("\"");
scrStr=scrStr.substr(beginPos+1,len);
int endPos=scrStr.find("\"");
str_temp=scrStr.substr(0,endPos);
return str_temp;
}
int Token(const char* pSep, char* pStr, std::vector& refvec)
{
for(char* outer = strtok(pStr, pSep) ; NULL != outer; outer = strtok(NULL, pSep))
{
refvec.push_back(outer);
}
return 0;
}
上述代碼在vs2008下運轉成果以下:
以上就是本文的全體內容,願望對年夜家闇練運用C++法式抓取數據有所贊助