Reading webpage iframe content in c#

最后都变了- 提交于 2019-12-11 11:17:15

问题


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:


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



来源:https://stackoverflow.com/questions/24325620/reading-webpage-iframe-content-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!