I am trying to figure out a way to get a image url from a web page source. I can get the web page source into a string and parse it line by line to find the line with the URL. However, I haven't been able to figure out a good way to pull just the URL from the line. I'm think this can be done with QRegExp, but have been unable to figure out how to use it.
Line I am trying to parse
<img width="980" height="1515" id="mainImg" src="//test/123.jpg" alt="test">
Final Working Code
void MainWindow::on_btnDownload_clicked()
{
QString url = "http://test.foo.com";
QUrl qURL = url;
QNetworkAccessManager manager;
QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url)));
QEventLoop event;
connect(response,SIGNAL(finished()),&event,SLOT(quit()));
event.exec();
QString html = response->readAll();
QStringList str;
str = html.split("\n");
//qDebug() << url;
for (int i = 0; i < str.size(); ++i){
if(str.at(i).contains("id=\"mainImg\"", Qt::CaseInsensitive)){
QString pic;
pic = str.at(i);
pic = pic.remove(QRegExp("<img[^>]*src=['|\"]", Qt::CaseInsensitive));
pic = pic.remove(QString::fromStdString("//"), Qt::CaseInsensitive);
pic = pic.remove('"');
pic = pic.remove("'");
pic = pic.remove('<');
pic = pic.remove('>');
pic = pic.remove(';');
pic = pic.left(pic.length()-1);
//qDebug() << str.at(i);
qDebug() << pic;
}
}
qDebug() << "Lines: " << str.size();
}