基于QT5的网络图片爬虫
以下是一个基于Qt5的简单网络图片爬虫示例代码。这个爬虫使用Qt的网络模块下载图片,并使用QThreadPool来并发下载图片。
#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QFile>
#include <QThreadPool>
#include <QRunnable>
#include <QUrl>
#include <QDebug>
class ImageCrawler : public QObject {
Q_OBJECT
public:
ImageCrawler(QNetworkAccessManager* manager, const QUrl& url)
: m_manager(manager), m_url(url) {}
void run() override {
QNetworkRequest request(m_url);
QNetworkReply* reply = m_manager->get(request);
QObject::connect(reply, &QNetworkReply::finished, [this, reply]() {
if (reply->error() == QNetworkReply::NoError) {
QFile file(QString("image_%1.jpg").arg(m_imageIndex++));
if (file.open(QIODevice::WriteOnly)) {
file.write(reply->readAll());
file.close();
qDebug() << "Image saved successfully";
} else {
qDebug() << "Cannot open file for writing";
}
} else {
qDebug() << "Error downloading image:" << reply->errorString();
}
reply->deleteLater();
});
}
signals:
void finished();
private:
QNetworkAccessManager* m_manager;
QUrl m_url;
int m_imageIndex = 0;
};
class ImageCrawlerPool : public QObject {
Q_OBJECT
public:
ImageCrawlerPool(QNetworkAccessManager* manager, const QList<QUrl>& urls, int threadCount = 4)
: m_manager(manager), m_urls(urls), m_threadCount(threadCount) {
QThreadPool::globalInstance()->setMaxThreadCount(m_threadCount);
}
void startCrawling() {
for (const QUrl& url : m_urls) {
ImageCrawler* crawler = new ImageCrawler(m_manager, url);
QObject::connect(crawler, &ImageCrawler::finished, [crawler]() {
crawler->deleteLater();
});
QThreadPool::globalInstance()->start(crawler);
}
}
private:
QNetworkAccessManager* m_manager;
QList<QUrl> m_urls;
int m_threadCount;
};
int main(int argc, ch
评论已关闭