创建.cpp/.h文件,复制以下代码即可
//
// Created by Administrator on 2023/3/12.
//
#include <QRegularExpressionMatchIterator>
#include "NetU.h"
void NetU::query(QNetworkReply* reply){
QString input = reply->readAll();
QRegularExpression dateRegex("(?:<div class=\"two\")[\\s\\S]*?(?:</div>)");
QRegularExpressionMatchIterator d = dateRegex.globalMatch(input);
QRegularExpressionMatch match = d.next();
QString word = match.captured(0);
this->getSerAndPackInfo(word);
this->getSummerize(word);
QRegularExpression dateRegex2("(?:<div class=\"two-tit\")[\\s\\S]*?(?:</div>)");
QRegularExpressionMatchIterator d2 = dateRegex2.globalMatch(input);
QRegularExpressionMatch match2 = d2.next();
QString word3 = match2.captured(0);
this->getName(word3);
this->getType(word3); //
QRegularExpression dateRegex3("(?<= xpath=\")[s\\S]*?(?=\")");
QRegularExpressionMatchIterator d3 = dateRegex3.globalMatch(input);
QRegularExpressionMatch match3 = d3.next();
this->URLs = match3.captured(0);
qDebug() << this->URLs;
}
void NetU::getSerAndPackInfo(QString div){
QRegularExpression getsd("(?:<span title=\")[\\s\\S]*?(?:>)");
QRegularExpressionMatchIterator c = getsd.globalMatch(div.replace("\n",""));
int count = 0;
while (c.hasNext()){
QRegularExpressionMatch cm = c.next();
if (count == 0){
this->serID = cm.captured(0).replace("<span title=\"","").replace("\">","");
}else{
this->packageID = cm.captured(0).replace("<span title=\"","").replace("\">","");
}
count ++;
}
qDebug() << this->serID;
qDebug() << this->packageID;
}
void NetU::getName(QString div){
QRegularExpression getsd("(?:\\)\">)[\\s\\S]*?(?:</a>)");
QRegularExpressionMatchIterator c = getsd.globalMatch(div.replace("\n",""));
int count = 0;
while (c.hasNext()){
QRegularExpressionMatch cm = c.next();
if (count == 0){
QRegularExpression gettp("(?<=>)[\\s\\S]*?(?=</a)");
QRegularExpressionMatch d = gettp.match(cm.captured(0));
this->name = d.captured(0).replace(" ","");
}else{
}
count ++;
}
qDebug() << this->name;
}
void NetU::getSummerize(QString div){
QRegularExpression getsd("(?:div class=\"multi)[\\s\\S]*?(?:/div)");
QRegularExpressionMatchIterator c = getsd.globalMatch(div.replace("\n",""));
int count = 0;
while (c.hasNext()){
QRegularExpressionMatch cm = c.next();
if (count == 0){
QRegularExpression gettp("(?<=</span>)[\\s\\S]*?(?=</div)");
QRegularExpressionMatch d = gettp.match(cm.captured(0));
this->Summarize = d.captured(0).replace(" ","");
}else{
}
count ++;
}
qDebug() << this->Summarize;
}
void NetU::getType(QString div){
QRegularExpression getsd("(?: class=\"catalog)[\\s\\S]*?(?:</a>)");
QRegularExpressionMatchIterator c = getsd.globalMatch(div.replace("\n",""));
int count = 0;
while (c.hasNext()){
QRegularExpressionMatch cm = c.next();
if (count == 0){
QRegularExpression gettp("(?<=>)[\\s\\S]*?(?=<)");
QRegularExpressionMatch d = gettp.match(cm.captured(0));
this->type = d.captured(0).replace(" ","");
}else{
}
count ++;
}
//(?<=>)[\s\S]*?(?=<)
qDebug() << this->type;
}
NetU::NetU() {
this->NETM = new QNetworkAccessManager();
QObject::connect(this->NETM,SIGNAL(finished(QNetworkReply*)), this,SLOT(query(QNetworkReply*)));
}
void NetU::GetComponentsInfo(QString ID) {
QNetworkRequest request(QUrl("https://so.szlcsc.com/global.html?k="+ID+"&hot-key=SS8050"));
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
this->NETM->get(request);
}
// // Created by Administrator on 2023/3/12. // #ifndef PACHONG_NETU_H #define PACHONG_NETU_H #include <QObject> #include <QNetworkReply> class NetU : public QObject { Q_OBJECT public: NetU(); QString serID; QString packageID; QString Summarize; QString name; QString type; QString URLs; void GetComponentsInfo(QString ID); public slots: void query(QNetworkReply *reply); void getSerAndPackInfo(QString div); void getName(QString div); void getSummerize(QString div); void getType(QString div); private: QNetworkAccessManager *NETM; }; #endif //PACHONG_NETU_H
使用方法:
NetU *unity = new NetU(); unity->GetComponentsInfo("C"); qDebug()<< "这个器件叫做:"<<unity->name;
输出的信息是:
"TPM3080PK3-3"
"TO-252"
""
"TPM3080PK3-3"
"场效应管(MOSFET)"
"https://alimg.szlcsc.com/upload/public/product/breviary//B9F531A2FEFEEEFE43F2AC6.jpg"
如果直接加入项目,请加入一个signals,当获取完emit 一下这个signals.
然后在使用的地方 Connect 一下这个信号~~
今天的文章 Qt 爬虫 爬取LCSC的器件信息(仅供学习,请勿违法)分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/87806.html