Reading webpage iframe content in c#

2019-08-03 17:12发布

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");
   }

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

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

1条回答
男人必须洒脱
2楼-- · 2019-08-03 17:42

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);
}

For the regex you could use this Regular Expression to get the SRC of images in C#

Edit :

        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));
        }

You really get the iframe source with this code

Edit2 :

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 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

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

查看更多
登录 后发表回答