CodeQString NetWork::getWebSource(QUrl url)
{
QNetworkAccessManager manager;
QEventLoop loop;
QNetworkReply *reply;
qDebug() << "Reading html code form " << url;
reply = manager.get(QNetworkRequest(url));
//請求結束並下載完成後,退出子事件循環
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
//開啓子事件循環
loop.exec();
QByteArray codeContent = reply->readAll();
//將獲取到的網頁源碼寫入文件
//必定要注意編碼問題,不然很容易出現亂碼的
return QTextCodec::codecForHtml(codeContent)->toUnicode(codeContent);
}
CodeqDebug() << getWebSource(QUrl("http://www.baidu.com/"));