获取网页源码的小例子,代码很简单,就不多作解释了。
不过一定要注意网页的编码问题,否则会出现乱码的!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
/* * 获取网页源码的小例子,代码很简单,就不多作解释了。 * 不过一定要注意网页的编码问题,否则会出现乱码的!!! */ #include <QCoreApplication> #include <QNetworkAccessManager> #include <QNetworkRequest> #include <QNetworkReply> #include <QUrl> #include <QEventLoop> #include <QFile> #include <QTextCodec> //网页地址 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.toLatin1()); codeContent = codec->toUnicode(codeContent.toLatin1()); out.setCodec(codec); out << codeContent << endl; file.close(); qDebug() << "Finished, the code have written to " << FILE_NAME; return 0; } |
发表评论
要发表评论,您必须先登录。