分类: C/C++
2013-07-24 17:01:49
获取网页源码的小例子,代码很简单,就不多作解释了。
[cpp]
#include
#include
//网页地址
const QString URLSTR = "";
//储存网页代码的文件
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;
}