在C#中读取网页iframe内容 [英] Reading webpage iframe content in c#

查看:436
本文介绍了在C#中读取网页iframe内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been recently working in downloading webpage content using WebClient in C#. The DownloadString method of WebClient can not download the content from iframe.

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

The short code for downloading content has been used as:

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

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

What should I need to use for reading iframe content in C#?

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

For Testing, I am using http://multiprofits.co.uk/oddsmatcher.html site which has iframe in it.

推荐答案

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

You have to search for the iframe tag in the main page and then take the src attribute to download the page in the 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

For the regex you could use this 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来源

You really get the iframe source with this code

Edit2:

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

I have found your problem. It's a security issue from the site. Launch the iframe url in a new browser you will receive this message :

oddsmatcher不允许在此域名上运行 [v2.oddsmatcher-data.co.uk/v2.oddsmatcher-data.co.uk] 有关更多详细信息,请cotact support@oddsmonkey.com

oddsmatcher is not permitted to run on this domain name [v2.oddsmatcher-data.co.uk/v2.oddsmatcher-data.co.uk] For more details please cotact support@oddsmonkey.com

因此,您不能直接下载iframe源.您可能必须使用WebBrowser或类似的东西

So you must can't download directly the iframe source. You probably have to use WebBrowser or something like this

这篇关于在C#中读取网页iframe内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆