【问题标题】:Can i read iframe through WebClient (i want the outer html)?我可以通过 WebClient 阅读 iframe(我想要外部 html)吗?
【发布时间】:2013-01-03 22:33:45
【问题描述】:

好吧,我的程序正在读取一个 Web 目标,该目标在正文的某处有我想要读取的 iframe。

我的 html 源代码

<html>
...
<iframe src="http://www.mysite.com" ></iframe>
...
</html>

在我的程序中,我有一个方法将源作为字符串返回

public static string get_url_source(string url)
{
   using (WebClient client = new WebClient())
   {
       return client.DownloadString(url);
   }
}

我的问题是我想在读取源代码时获取 iframe 的源代码,就像在正常浏览中一样。

我只能通过使用WebBrowser Class 来做到这一点,还是有办法在 WebClient 甚至其他类中做到这一点?

真正的问题: 给定一个 url,我如何获取外部 html?欢迎任何方法。

【问题讨论】:

  • 猜你可以通过java脚本访问url ..
  • 请注意,如果您从另一个域访问页面,您可能会遇到跨站点脚本的安全问题。
  • 是的 iframe 来自另一个域,但为什么会出现问题?
  • @Incognito:在 wiki 上查看 [Cross-site scripting]
  • 实际上我的页面涉及脚本,经过一番研究,我发现迄今为止最接近的解决方案是这样 > webBrowser1.Document.ActiveElement.OuterHtml.ToString();所以我需要页面的外部html。另一个类似的问题在这里 -> stackoverflow.com/questions/10562051/…

标签: c# browser html-parsing webclient


【解决方案1】:

经过一番搜索,我找到了答案,这就是我想要的

webBrowser1.Url = new Uri("http://www.mysite.com/");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
string InnerSource = webBrowser1.Document.Body.InnerHtml; 
                            //You can use here OuterHtml too.

【讨论】:

    【解决方案2】:

    使用 HTML Agility Pack 解析您的源代码,然后:

    List<String> iframeSource = new List<String>();
    
    HtmlDocument doc = new HtmlDocument();
    doc.Load(url);
    
    foreach (HtmlNode node in doc.DocumentElement.SelectNodes("//iframe"))
        iframeSource.Add(get_url_source(mainiFrame.Attributes["src"]));
    

    如果您的目标是单个 iframe,请尝试使用 ID 属性或其他方式来识别它,以便您只能检索一个来源:

    String iframeSource;
    
    HtmlDocument doc = new HtmlDocument();
    doc.Load(url);
    
    foreach (HtmlNode node in doc.DocumentElement.SelectNodes("//iframe"))
    {
        // Just an example for check, but you could use different approaches...
        if (node.Attributes["id"].Value == 'targetframe')
            iframeSource = get_url_source(node.Attributes["src"].Value);
    }
    

    【讨论】:

    • 是的,我针对的是一个 iframe,我试试你的例子,然后会回复。
    【解决方案3】:

    获取网站源码后,可以使用HtmlAgilityPack获取iframe的url

    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    doc.LoadHtml(html);
    
    var src = doc.DocumentNode.SelectSingleNode("//iframe")
                .Attributes["src"].Value;
    

    然后再拨打get_url_source

    【讨论】:

    • 这会导致 iframe 更改其数据。
    • @Incognito, change its data?这是什么意思?怎么样?
    • 因为 iframe 加载新数据的时间是生成的,所以我想要第一次加载页面时生成的数据。就像有一个在 iframe 中生成随机数的脚本。
    • @Incognito That would cause the iframe to change its data,我认为正确的短语是“这将导致 iframe not 更改其数据”
    • 我的意思是,正如您在回答中所说,第二次调用 iframe 的 src,我已经尝试过了。每次访问 iframe 的 src 时,它都会更改其数据。因此,如果我使用您的方法找出 iframe 的 src 并调用 get_url_source,那将再次调用 iframe,结果将生成不可用的新数据。这就是为什么我想在第一次调用我的页面时获取 iframe 的源/数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多