QT实现CSDN上传资源管理助手Demo之(3)请求上传资源页面并解析

QT实现CSDN上传资源管理助手Demo之(3)请求上传资源页面并解析

请求自己上传的资源页面,只需要请求http://download.csdn.net/my/uploads/就可以了。在请求结果中解析自己的账户相关的信息,比如等级和分数。
发起请求:
void QtResourceAssistant::downloadInfoRequest(int Page)
{
	connect(mNetworkAccessManager, &QNetworkAccessManager::finished, this, &QtResourceAssistant::downloadInfoReply);

	QNetworkRequest NetworkRequest(QUrl("http://download.csdn.net/my/uploads/" + QString::number(Page)));
	NetworkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
	NetworkRequest.setRawHeader(QByteArray("Referer"), QByteArray("http://download.csdn.net/"));
	NetworkRequest.setRawHeader(QByteArray("Cookie"), mCookies.toLatin1());

	mNetworkAccessManager->get(NetworkRequest);
}
接收请求信息:
void QtResourceAssistant::downloadInfoReply(QNetworkReply *Reply)
{
	disconnect(mNetworkAccessManager, &QNetworkAccessManager::finished, this, &QtResourceAssistant::downloadInfoReply);

	QString strReply = Reply->readAll();
	Reply->deleteLater();
	if (strReply.isEmpty())
	{
		QMessageBox::warning(this, tr("警告"), tr("获取下载信息失败,请检查网络并重启软件"), QMessageBox::StandardButton::Close);
		return;
	}

	//mDownloadInfoWidget->setAvatarImage(loadInternetImage(mUserInfoDialog.getAvatar()));
	mDownloadInfoWidget->setNickName(mUserInfoDialog.getNickName());
	mDownloadInfoWidget->parseDownloadInfo(strReply);
}
解析请求得到的网页信息:
void QDownloadWidget::parseDownloadInfo(QString strInfo)
{
	if (strInfo.isEmpty())
		return;
	QRegExp rep("http://c.csdnimg.cn/jifen/images/xunzhang/jianzhang/(\\w+).png");
	rep.indexIn(strInfo);

	QString TempString = rep.cap();
	emit loadLevelImageSignal(TempString);

	rep.setPattern("(.|\n)+MB");
	rep.indexIn(strInfo);
	TempString = rep.cap().remove("").remove("\n").remove(" ");
	mDownloadSizeLabel->setText(tr(QString("上传权限:" + TempString).toStdString().c_str()));

	rep.setPattern("积分(\\d+)");
	rep.indexIn(strInfo);
	TempString = rep.cap().remove("积分");
	mDownloadPointsLabel->setText("
积分

"+TempString+"

"); rep.setPattern("总排名(\\d+)"); rep.indexIn(strInfo); TempString = rep.cap().remove("总排名"); mRankLabel->setText("
总排名

" + TempString + "

"); rep.setPattern("上传资源(\\d+)"); rep.indexIn(strInfo); TempString = rep.cap().remove("上传资源"); mDownloadResourceLabel->setText("
上传资源

" + TempString + "

"); rep.setPattern("下载资源(\\d+)"); rep.indexIn(strInfo); TempString = rep.cap().remove("下载资源"); mUploadResourceLabel->setText("
下载资源

" + TempString + "

"); rep.setPattern("创建专辑(\\d+)"); rep.indexIn(strInfo); TempString = rep.cap().remove("创建专辑"); mCreateAlbumLabel->setText("
创建专辑

" + TempString + "

"); parseUploadResources(strInfo); } void QDownloadWidget::parseUploadResources(QString strInfo) { if (strInfo.isEmpty()) return; mUploadResourceWidget->clear(); //每页5个 for (int i = 0; i < 5; i++) { QRegExp rep("/download/(\\w+/\\w+)"); rep.indexIn(strInfo); QString TempString = rep.cap(); if (TempString.isEmpty()) break; QListWidgetItem *Item = new QListWidgetItem(mUploadResourceWidget); QResourceItemWidget *ItemWidget = new QResourceItemWidget(mUploadResourceWidget); Item->setSizeHint(QSize(840, 120)); connect(ItemWidget, &QResourceItemWidget::downloadResource, this, &QDownloadWidget::downloadResource); connect(ItemWidget, &QResourceItemWidget::deleteResource, this, &QDownloadWidget::deleteResource); mUploadResourceWidget->addItem(Item); mUploadResourceWidget->setItemWidget(Item, ItemWidget); ItemWidget->setResourceUrl(TempString); rep.setPattern("(.+)" + TempString + "\">"); rep.indexIn(strInfo); TempString = rep.cap(); strInfo.remove(TempString); TempString = strInfo.left(strInfo.indexOf("")); ItemWidget->setResourceName("

" + TempString + "

"); TempString = strInfo.left(strInfo.indexOf("brief\">")); strInfo.remove(TempString + "brief\">"); TempString = strInfo.left(strInfo.indexOf("

")).remove("\n"); ItemWidget->setDescription(TempString.left(45) + "..."); //标签最多5个 TempString = strInfo.left(strInfo.indexOf("下载个数:")) + "下载个数:"; strInfo.remove(TempString); TempString = parseTags(TempString); ItemWidget->setTags(TempString); rep.setPattern("\\d+"); rep.indexIn(strInfo); TempString = rep.cap(); ItemWidget->setDownloadTime("下载个数:" + TempString); TempString = strInfo.left(strInfo.indexOf("上传时间:")) + "上传时间:"; strInfo.remove(0, TempString.length()); rep.setPattern("(\\d|-)+"); rep.indexIn(strInfo); TempString = rep.cap(); ItemWidget->setUploadTime("上传时间:" + TempString); TempString = strInfo.left(strInfo.indexOf("所需积分/C币:")) + "所需积分/C币:"; strInfo.remove(0, TempString.length()); rep.setPattern("\\d+"); rep.indexIn(strInfo); TempString = rep.cap(); ItemWidget->setNeedScore("所需积分:" + TempString); } } QString QDownloadWidget::parseTags(QString strInfo) { QString TempTags = ""; for (int i = 0; i < 5; i++) { QString TempString = strInfo.left(strInfo.indexOf("/tag/")); strInfo.remove(TempString); if (strInfo.isEmpty()) break; TempString = strInfo.left(strInfo.indexOf(">")); strInfo.remove(TempString + ">"); TempString = strInfo.left(strInfo.indexOf("")); TempTags.append("<" + TempString + ">"); } return TempTags; }

得到的账户信息和上传资源列表, 主界面使用QMainWindow,列表显示使用QListWidget+自定义QWidget实现显示。如下图所示:
QT实现CSDN上传资源管理助手Demo之(3)请求上传资源页面并解析_第1张图片

并且实现了翻页功能,实现很简单只需要请求不同的页面就可以了。因为每一页的链接后面会有一个数字:

直接请求不同的链接,得到页面信息并解析就可以了:
QT实现CSDN上传资源管理助手Demo之(3)请求上传资源页面并解析_第2张图片

资源列表和账户信息的请求,解析,和显示大致就是这样。
声明:
QT实现CSDN上传资源管理助手Demo之(3)请求上传资源页面并解析_第3张图片

本文地址:http://blog.csdn.net/u011417605/article/details/78339470
交流QQ:1245178753
源码下载:https://github.com/xdd1990/QtResourceAssistant

你可能感兴趣的:(Qt,Qt学习和实战)