在c#中读取网页iframe内容

本文关键字:iframe 内容 网页 读取 | 更新日期: 2023-09-27 18:09:27

我最近一直在使用c#的WebClient下载网页内容。WebClient的DownloadString方法无法从iframe下载内容。

下载内容的短代码已被使用为:

   using (var client = new WebClient())
   {
        string html = client.DownloadString("url");
   }

在c#中我应该使用什么来读取iframe内容?

对于测试,我使用http://multiprofits.co.uk/oddsmatcher.html网站,其中有iframe。

在c#中读取网页iframe内容

您必须在主页中搜索iframe标签,然后使用src属性下载iframe

中的页面。
using (var client = new WebClient())
{
    string html = client.DownloadString("url");
    string src = ... //find iframe source with regex
    string iframe = client.DownloadString(src);
}

对于正则表达式,您可以使用此正则表达式来获取c#中图像的SRC

编辑:

        using (var client = new WebClient())
        {
            string html = client.DownloadString("http://multiprofits.co.uk/oddsmatcher.html");
            string src = Regex.Match(html, "<iframe.+?src=['"'](.+?)['"'].*?>", RegexOptions.IgnoreCase).Groups[1].Value;
            Console.Write(client.DownloadString(src));
        }

你真正得到的iframe的源代码是这个代码

Edit2:

我找到你的问题了。这是网站的安全问题。在新浏览器中启动iframe url,您将收到以下消息:

oddsmatcher不允许在此域名上运行[v2.oddsmatcher-data.co.uk/v2.oddsmatcher-data.co.uk]欲了解更多详情,请联系support@oddsmonkey.com

所以你不能直接下载iframe源代码。你可能需要使用WebBrowser之类的工具