获取网页源码的小例子,代码很简单,就不多作解释了。
不过一定要注意网页的编码问题,否则会出现乱码的!!!
- #include <QtCore>
- #include <QtNetwork>
- //网页地址
- const QString URLSTR = "http://www.csdn.net/";
- //储存网页代码的文件
- const QString FILE_NAME = "code.html";
- int main(int argc, char **argv)
- {
- QCoreApplication app(argc, argv);
- QUrl url(URLSTR);
- QNetworkAccessManager manager;
- QEventLoop loop;
- QTextCodec *codec;
- QNetworkReply *reply;
- qDebug() << "Reading html code form " << URLSTR;
- reply = manager.get(QNetworkRequest(url));
- //请求结束并下载完成后,退出子事件循环
- QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
- //开启子事件循环
- loop.exec();
- //获取源码,打开文件
- QFile file(FILE_NAME);
- if( !file.open(QIODevice::WriteOnly | QIODevice::Text) )
- {
- qDebug() << "Cannot open the file: " << FILE_NAME;
- return 0;
- }
- QTextStream out(&file);
- QString codeContent = reply->readAll();
- //将获取到的网页源码写入文件
- //一定要注意编码问题,否则很容易出现乱码的
- codec = QTextCodec::codecForHtml(codeContent.toAscii());
- codeContent = codec->toUnicode(codeContent.toAscii());
- out.setCodec(codec);
- out << codeContent << endl;
- file.close();
- qDebug() << "Finished, the code have written to " << FILE_NAME;
- return 0;
- }
http://blog.csdn.net/small_qch/article/details/7200271