QT:轻松获取网页源码
获取网页源码的小例子,代码很简单,就不多作解释了。
[cpp]
#include <QtCore>
#include <QtNetwork>
//网页地址
const QString URLSTR = "http://www.csdn.net/";
//储存网页代码的文件
const QString FILE_NAME = "code.txt";
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QUrl url(URLSTR);
QNetworkAccessManager manager;
QEventLoop loop;
qDebug() << "Reading code form " << URLSTR;
//发出请求
QNetworkReply *reply = manager.get(QNetworkRequest(url));
//请求结束并下载完成后,退出子事件循环
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
//开启子事件循环
loop.exec();
//将读到的信息写入文件
QString code = reply->readAll();
QFile file(FILE_NAME);
file.open(QIODevice::WriteOnly);
QTextStream out(&file);
out << code << endl;
file.close();
qDebug() << "Finished, the code have written to " << FILE_NAME;
return 0;
}
摘自 小豪之家
补充:软件开发 , 其他 ,