加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用C++程序获取新浪行情数据的方法

发布时间:2020-12-16 03:30:04 所属栏目:百科 来源:网络整理
导读:在日常开发中我们经常会使用到行情数据,很多的时候我们根据一个基准数据区构造行情,但是随着时间的推移然来构造的数据与真实行情数据之间的差距越来越大。 本问以AG1309为例子来说明,如何使用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,0);
 if(hSession != NULL)
 {
  HINTERNET hHttp = InternetOpenUrl(hSession,url,INTERNET_FLAG_DONT_CACHE,0);
  if (hHttp != NULL)
  {
   wprintf_s(_T("%sn"),url);
   char Temp[MAXSIZE];
   ULONG Number = 1;
   while (Number > 0)
   {
    InternetReadFile(hHttp,Temp,MAXSIZE - 1,&Number);
    Temp[Number] = '';
    std::string str= GetSubBtFind(Temp);
    ;
    char szpstr[1024];
    strcpy(szpstr,str.c_str());
    std::vector vec;
    Token(",",szpstr,vec);
    printf("%sn",Temp);

    for(int i=0; i
    {
      printf("%sn",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,std::vector& refvec)
{
 for(char* outer = strtok(pStr,pSep) ; NULL != outer; outer = strtok(NULL,pSep))
 {
  refvec.push_back(outer);
 }

 return 0;
}

上述代码在vs2008下运行结果如下:

以上就是本文的全部内容,希望对大家熟练应用C++程序抓取数据有所帮助

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读