获取网页源码的小例子,代码很简单,就不多作解释了。
不过一定要注意网页的编码问题,否则会出现乱码的!!!
- #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
QT:轻松获取网页源码
原文:http://www.cnblogs.com/findumars/p/4993551.html