You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C#从HTML中提取所有iframe的视频URL到字符串数组?

问题描述

我有一段HTML数据(如下),需要从中提取所有iframe的src链接,特别是其中的Vimeo视频URL,最终得到一个字符串数组。目前用Regex.Match只能获取第一个链接,无法拿到所有结果。

<h4> Daily Prayer Powered by Hallow App: </h4>
<center><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/videoseries?list=PLrM5hI29ZNuUmJ6wq15_-qUN8GAWxDs5l\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"\"></iframe></center>
<h3> The featured videos, the Queenship of Mary & Our Lady of Knock, can be found at the bottom of this page. </h3>

<h4>First Reading - Adapted from 2Thes. 2:1-3A, 14-17 </h4>
<p>Brothers and sisters: </p>
<p>If you hear a statement or a letter supposedly from us that the Lord Jesus Christ is coming now, do not be alarmed.  Do not let anyone trick you.  He has called you through the Gospel to receive the glory of our Lord Jesus.  So be strong and faithful to the traditions which you were taught by us, either orally or in writing.  May our Lord Jesus Christ and God our Father, who loves us and encourages us and gives us hope, encourage you and strengthen your hearts to do every good work. </p>

<h4>Psalm 96:10-13 </h4>
<p>Say among the nations, "The LORD reigns! </p>
<p>    Yea, the world is established, it shall never be moved; </p>
<p>    he will judge the peoples with equity."</p>
<p>Let the heavens be glad, and let the earth rejoice; </p>
<p>    let the sea roar, and all that fills it; </p>
<p>    let the field exult, and everything in it! </p>
<p>Then shall all the trees of the wood sing for joy </p>
<p>    before the LORD, for he comes, </p>
<p>    for he comes to judge the earth. </p>
<p>He will judge the world with righteousness, </p>
<p>    and the peoples with his truth. </p>

<h4>Gospel - Adapted from Matthew 23:23-26 </h4>
<p>Jesus said: </p>
<p>"Great sadness to you, scribes and Pharisees, you hypocrites!  You give valuable spices to the temple, but you do not give more important things - justice, and mercy, and faithfulness.  You should have done these as well as the others.  You are blind guides who focus on religious details but ignore large sins.  You appear clean on the outside, but inside you are full of selfishness.  Blind Pharisee, first be clean on the inside, and the outside will also be clean."</p>

<h4>Featured Videos:</h4>
<h3> The Queenship of Mary </h3>
<p> Watch the Catholic Brain video the Queenship of Mary below: </p>
<div class=\"video-responsive\"><iframe src=\"https://player.vimeo.com/video/447623879\" width=\"640\" height=\"360\" frameborder=\"0\" webkitallowfullscreen=\"\" mozallowfullscreen=\"\" allowfullscreen=\"\"></iframe></div>
<p>The Catholic Brain lesson the Queenship of Mary can be <a href=\"https://www.catholicbrain.com/edu-lesson/1053474/video/the-queenship-of-mary\" target=\"_blank\">found here</a>.</p>
<h3> Our Lady of Knock </h3>
<p> Watch the Catholic Brain video Our Lady of Knock below: </p>
<div class=\"video-responsive\"><iframe src=\"https://player.vimeo.com/video/447228009\" width=\"640\" height=\"360\" frameborder=\"0\" webkitallowfullscreen=\"\" mozallowfullscreen=\"\" allowfullscreen=\"\"></iframe></div>
<p>The Catholic Brain lesson Our Lady of Knock can be <a href=\"https://www.catholicbrain.com/edu-lesson/1051554/video/our-lady-of-knock\" target=\"_blank\">found here</a>.</p>

我当前的代码只能获取第一个iframe的src:

string src = Regex.Match(description, "&lt;iframe.+?src=[\"'](.+?)[\"'].*?&gt;", RegexOptions.IgnoreCase).Groups[1].Value;
Debug.WriteLine("src:&gt;&gt;" + src);

解决方案

方法1:改进正则表达式,获取所有匹配结果

Regex.Match仅返回第一个匹配项,要获取所有结果需使用Regex.Matches遍历所有匹配组。可以根据需求提取全部iframe的src,或者仅过滤Vimeo链接。

提取所有iframe的src:

string html = "你的HTML字符串";
var matches = Regex.Matches(html, @"<iframe.+?src=[""'](.+?)[""'].*?>", RegexOptions.IgnoreCase | RegexOptions.Singleline);

List<string> videoUrls = new List<string>();
foreach (Match match in matches)
{
    if (match.Groups[1].Success)
    {
        videoUrls.Add(match.Groups[1].Value);
    }
}

// 转换为字符串数组
string[] urlArray = videoUrls.ToArray();

// 输出结果
foreach (string url in urlArray)
{
    Debug.WriteLine("视频URL:" + url);
}

仅提取Vimeo视频URL:

如果只需要Vimeo链接,可在正则中加入域名匹配:

string html = "你的HTML字符串";
var matches = Regex.Matches(html, @"<iframe.+?src=[""'](https://player\.vimeo\.com/video/\d+)[""'].*?>", RegexOptions.IgnoreCase | RegexOptions.Singleline);

List<string> vimeoUrls = new List<string>();
foreach (Match match in matches)
{
    if (match.Groups[1].Success)
    {
        vimeoUrls.Add(match.Groups[1].Value);
    }
}

string[] vimeoUrlArray = vimeoUrls.ToArray();

方法2:使用HTML解析库(推荐)

正则处理HTML容易出现边缘问题(如属性顺序变化、特殊字符干扰),更可靠的方式是使用专业HTML解析库,比如HtmlAgilityPack。

步骤:

  1. 通过NuGet安装HtmlAgilityPack:Install-Package HtmlAgilityPack
  2. 编写代码提取目标链接:
using HtmlAgilityPack;

string html = "你的HTML字符串";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);

var iframeNodes = doc.DocumentNode.SelectNodes("//iframe");
List<string> videoUrls = new List<string>();

if (iframeNodes != null)
{
    foreach (var iframe in iframeNodes)
    {
        string src = iframe.GetAttributeValue("src", "");
        if (!string.IsNullOrEmpty(src))
        {
            // 仅保留Vimeo链接
            if (src.StartsWith("https://player.vimeo.com/video/"))
            {
                videoUrls.Add(src);
            }
            // 若需要所有iframe的src,注释上面的判断,取消下面的注释
            // videoUrls.Add(src);
        }
    }
}

string[] urlArray = videoUrls.ToArray();

// 输出结果
foreach (string url in urlArray)
{
    Debug.WriteLine("视频URL:" + url);
}

内容的提问来源于stack exchange,提问作者Sreejith Sree

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 11:15:43