用于HTML图像标记的QRegExp
QRegExp for HTML Image Tags
首先,我只想说,我理解将regexs用于HTML是个坏主意。我只是用它来获取<img>
标签信息,所以我不在乎嵌套等。
话虽如此,我正在尝试获取网页中所有图像的src
URL。然而,我似乎只得到了第一个结果。这是我的正则表达式,还是我使用它的方式?我的正则表达式技能有点生疏,所以我可能缺少一些明显的东西。
QRegExp imgTagRegex("(<img.*>)+", Qt::CaseInsensitive); //Grab the entire <img> tag
imgTagRegex.setMinimal(true);
imgTagRegex.indexIn(pDocument);
QStringList imgTagList = imgTagRegex.capturedTexts();
imgTagList.removeFirst(); //the first is always the total captured text
foreach (QString imgTag, imgTagList) //now we want to get the source URL
{
QRegExp urlRegex("src="(.*)"", Qt::CaseInsensitive);
urlRegex.setMinimal(true);
urlRegex.indexIn(imgTag);
QStringList resultList = urlRegex.capturedTexts();
resultList.removeFirst();
imageUrls.append(resultList.first());
}
当我进入foreach
循环时,imgTagList
只包含1个字符串。对于"古埃及的猫"维基百科页面,它包含:
<img alt="" src="//upload.wikimedia.org/wikipedia/commons/thumb/1/13/Egypte_louvre_058.jpg/220px-Egypte_louvre_058.jpg" width="220" height="407" class="thumbimage" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/1/13/Egypte_louvre_058.jpg/330px-Egypte_louvre_058.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/1/13/Egypte_louvre_058.jpg 2x" />
这就是我想要的,但我知道页面上有更多的图像标签。。。你知道我为什么只得到第一个回来吗?
更新
在Sebastian Lange的帮助下,我走到了这一步:
QRegExp imgTagRegex("<img.*src="(.*)".*>", Qt::CaseInsensitive);
imgTagRegex.setMinimal(true);
QStringList urlMatches;
QStringList imgMatches;
int offset = 0;
while(offset >= 0)
{
offset = imgTagRegex.indexIn(pDocument, offset);
offset += imgTagRegex.matchedLength();
QString imgTag = imgTagRegex.cap(0);
if (!imgTag.isEmpty())
imgMatches.append(imgTag); // Should hold complete img tag
QString url = imgTagRegex.cap(1);
if (!url.isEmpty())
{
url = url.split(""").first(); //ehhh....
if (!urlMatches.contains(url))
urlMatches.append(url); // Should hold only src property
}
}
末尾的split
是一种去除<img>
标记中非src元素的方法,因为看起来我无法只获取src="..."
段中的数据。它是有效的,但这只是因为我无法找到正确的方法。我还添加了一些东西来标准化
QRegExp通常只给出一个匹配。列表capturedTexts()给出了这一匹配的所有捕获!在一个regex语句中可以有多个捕获括号。为了解决你的问题,你需要做一些类似的事情:
QRegExp imgTagRegex("\<img[^\>]*src\s*=\s*"([^"]*)"[^\>]*\>", Qt::CaseInsensitive);
imgTagRegex.setMinimal(true);
QStringList urlmatches;
QStringList imgmatches;
int offset = 0;
while( (offset = imgTagRegex.indexIn(pDocument, offset)) != -1){
offset += imgTagRegex.matchedLength();
imgmatches.append(imgTagRegex.cap(0)); // Should hold complete img tag
urlmatches.append(imgTagRegex.cap(1)); // Should hold only src property
}
EDIT:已将捕获RegExpression更改为"\<img[^\>]*src="([^"]*)"[^\>]*\>"
EDIT2:在src字符串中添加了可能的空格:"\<img[^\>]*src\s*=\s*"([^"]*)"[^\>]*\>"
相关文章:
- 简单的 HTML 验证器
- 如何在 html 页面中插入 cgi 应用程序的输出?
- 在 C++ 中编写解析器来解析给定的 HTML 实体
- 使用 ReadTheOrg 将识字程序导出为 HTML 时保留源块的内容
- 当我使用 QDomDocument 解析 html 数据时,如何保留 "?
- 如何使用 libcurl 在本地 IP 上获取 HTML?
- 通过 c++ 中的套接字发送 html
- 如何使用utf-8编码在Qt5 WebEngineView中加载HTML页面?
- C++ libcurl - 无法从 URL 检索整个 html 内容
- 使用 C++ std::sregex_token_iterator 提取 HTML 注释
- 检查HTML 5视频元素是否正在获取帧
- 如何使用PHP将上传到本地主机的一个html文件重定向到磁盘上的另一个html文档
- Flex/Bison标记为HTML程序
- 如何在C++中正确地将我的语言字符输出到HTML文件中
- 使用 Lamda 从 QWebEngineView 中的 QWebEnginePage 获取 HTML
- 根据 C++ 中生成的随机数的值生成 Html RGB 值
- 正则表达式忽略 HTML 注释
- qprint 和 QTextDocument 中的表格边距使用 html 作为输入
- 如何从 doxygen 创建的 html 更改回代码
- 用于HTML图像标记的QRegExp