【问题标题】:QRegExp for HTML Image Tags用于 HTML 图像标签的 QRegExp
【发布时间】:2013-07-17 02:05:43
【问题描述】:

首先,我只想说,我知道对 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 是一种摆脱&lt;img&gt; 标记中非src 元素的巧妙方法,因为看起来我无法仅获取src="..." 段内的数据。它有效,但这只是因为我无法以正确的方式让它工作。我还添加了一些东西来标准化

【问题讨论】:

    标签: c++ regex qt qregexp


    【解决方案1】:

    QRegExp 通常只给出一个匹配项。列表 captureTexts() 给出了这一匹配的所有捕获!一个正则表达式语句中可以有多个捕获括号。要解决您的问题,您需要执行以下操作:

    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
    }
    

    编辑:将捕获 RegExpression 更改为 "\\&lt;img[^\\&gt;]*src=\"([^\"]*)\"[^\\&gt;]*\\&gt;" EDIT2:在 src 字符串中添加了可能的空格:"\\&lt;img[^\\&gt;]*src\\s*=\\s*\"([^\"]*)\"[^\\&gt;]*\\&gt;"

    【讨论】:

    • 谢谢 Sebastian,我会试一试并回复您。
    • 看起来indexIn 在第一次运行时返回了-1
    • 尝试使用"\\&lt;img[^\\&gt;]*\\&gt;"查看是否找到img标签,然后展开为:"\\&lt;img[^\\&gt;]*src=\"([^\"])*\"[^\\&gt;]*\\&gt;"
    • 我自己快速检查了一下,新的正则表达式正确地捕获了 src,你现在应该可以接受这个解决方案了。至少对于给定的 img 标签它工作正常。如果src = "url" 之间可能有空格,您最终希望将其替换为:"\\&lt;img[^\\&gt;]*src\\s*=\\s*\"([^\"]*)\"[^\\&gt;]*\\&gt;"
    • 最后一行之前的两行好像少了一个括号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多