QT每日下载沪深A股日线数据
.proNameListCatch.hnamelistcatch.cppmain.cpp代码说明
废话不多说,先上代码。
.pro
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = get_hs_a_name_list
TEMPLATE = app
SOURCES += main
.cpp\
namelistcatch
.cpp
HEADERS += namelistcatch
.h
FORMS += namelistcatch
.ui
NameListCatch.h
#ifndef
NAMELISTCATCH_H
#define
NAMELISTCATCH_H
#include
<QMainWindow
>
#include
<QDebug
>
#include
<QTimer
>
#include
<QDateTime
>
#include
<QNetworkAccessManager
>
#include
<QNetworkReply
>
#include
<QNetworkRequest
>
#include
<QFile
>
namespace Ui
{
class NameListCatch;
}
class NameListCatch : public QMainWindow
{
Q_OBJECT
public:
explicit
NameListCatch(QWidget
*parent
= 0);
~NameListCatch();
void StockListCatch();
public:
QTimer
*mytimer
;
QNetworkAccessManager
*NetAccessManager
;
unsigned short file_recv_flag
;
QString current_date
;
private slots
:
void on_mytimer_task();
void on_recv_data(QNetworkReply
*reply
);
private:
Ui
::NameListCatch
*ui
;
};
#endif
namelistcatch.cpp
#include
"namelistcatch.h"
#include
"ui_namelistcatch.h"
NameListCatch
::NameListCatch(QWidget
*parent
) :
QMainWindow(parent
),
ui(new Ui::NameListCatch
)
{
ui
->setupUi(this);
file_recv_flag
=0;
mytimer
= new QTimer(this);
connect(mytimer
, SIGNAL(timeout()), this, SLOT(on_mytimer_task()));
mytimer
->start(60000);
NetAccessManager
= new QNetworkAccessManager(this);
connect(NetAccessManager
, SIGNAL(finished(QNetworkReply
*)), this, SLOT(on_recv_data(QNetworkReply
*)));
}
NameListCatch
::~NameListCatch()
{
delete ui
;
}
void NameListCatch
::on_mytimer_task()
{
QDateTime current_date_time
=QDateTime
::currentDateTime();
current_date
=current_date_time
.toString("yyyy.MM.dd hh:mm:ss.zzz ddd");
unsigned short hour
= current_date
.mid(11,2).toInt();
if(hour
==0)
{
file_recv_flag
= 0;
}
if((hour
==17)&&(file_recv_flag
==0))
{
qDebug()<<"start catch StockList file!"<<current_date
;
StockListCatch();
file_recv_flag
=1;
}
}
void NameListCatch
::StockListCatch()
{
QString url_str
= "http://stock.gtimg.cn/data/get_hs_xls.php?id=ranka&type=1&metric=chr";
QUrl
url(url_str
);
NetAccessManager
->get(QNetworkRequest(url
));
}
void NameListCatch
::on_recv_data(QNetworkReply
*reply
)
{
QString filename
;
QString path
= "//data//namelist//";
QString year
= current_date
.mid(0,4);
QString month
= current_date
.mid(5,2);
QString date
= current_date
.mid(8,2);
filename
= path
+year
+"_"+month
+"_"+date
+".xls";
QByteArray recv_data
= reply
->readAll();
reply
->deleteLater();
QFile
recvfile(filename
);
recvfile
.open(QIODevice
::WriteOnly
);
recvfile
.write(QByteArray((const char
*)recv_data
, recv_data
.size()));
recvfile
.close();
}
main.cpp
#include
"namelistcatch.h"
#include
<QApplication
>
int
main(int argc
, char
*argv
[])
{
QApplication
a(argc
, argv
);
NameListCatch w
;
w
.show();
return a
.exec();
}
代码说明
以上随意的采用widget模式,使用定时器定时检测系统时间,当时间达到17:00后,从腾讯证券请求下载沪深两市A股日线数据的程序,源数据为xls文件,这里不作处理直接保存到指定路径下。