【问题标题】:Reading webpage iframe content in c#在 C# 中读取网页 iframe 内容
【发布时间】:2014-06-20 10:52:55
【问题描述】:

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

下载内容的短代码已用作:

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

在 C# 中读取 iframe 内容需要什么?

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

【问题讨论】:

  • 要么使用HtmlAgilityPack手动解析内容,然后用另一个DownloadString请求加载iframe,要么使用WebBrowser(支持much more complex web scrapping scenarios)。
  • 这里的问题是从另一个DownloadString获取的iframe内容在原始网页中显示不正确。
  • @akash88,然后使用WebBrowser,点击我发布的链接。
  • @PaulZahra :问题与该解决方案相同。

标签: c# html iframe web-scraping webclient


【解决方案1】:

你必须在主页面中搜索iframe标签,然后在iframe中取src属性下载页面

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

对于正则表达式,您可以使用 Regular Expression to get the SRC of images in C#

编辑:

        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 源代码

编辑2:

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

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

所以你一定不能直接下载 iframe 源。您可能必须使用 WebBrowser 或类似的东西

【讨论】:

  • 我也是这样做的。但 iframe 内容并不是它在网页中显示的内容。
  • 我不明白。 iframe src 是显示页面的 url。因此,如果您下载此页面,您将拥有 iframe 内容。
  • 如果 iframe 页面包含 css、javascript、... 您也必须下载它们才能正确显示页面。所以你最好使用工具
  • 您不会从该 iframe 源 url 获得实际的 iframe 内容。我已经试过了:(
  • 感谢您的努力。我已经有了 iframe 源 URL。那不是问题。主要问题是我无法使用该源 URL 获得正确的 iframe 内容。
猜你喜欢
  • 2011-10-03
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多