c#用iftlagilitypack获取iframe kod?

时间:2018-03-18 09:49:24

标签: c# iframe html-agility-pack

我正在使用带有HtmlAgilityPack的C#,我希望通过网站的HTML获取iframe。

这是iframe区域:

<div class="playercont">
   <div id="singlePlay" class="dp_player" data-o="" data-s="">
      <p><iframe src="video.com/sadas" scrolling="no" frameborder="0" width="640" height="360" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe></p>
   </div>
</div>

我想通过控制台或应用程序获取iframe, 我想要的输出是:

<iframe src="video.com/sadas" scrolling="no" frameborder="0" width="640" height="360" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>

请给我一些例子或确切的代码。

****更新:几分钟后,我得到了一些错误的代码 这是代码:

var url = "my html url";

var httpClient = new HttpClient();
var html =  await httpClient.GetStringAsync(url);
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);


HtmlNode divContainer = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='someclass']");


Console.WriteLine(divContainer.InnerHtml);

上面的代码给了我这个:

<p><iframe src="video.com/sadas" scrolling="no" frameborder="0" width="640" height="360" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe></p>

最后,我想排除&#34; p&#34;从输出标记,请帮助我。

1 个答案:

答案 0 :(得分:0)

您应该能够从divContainer节点获取iframe,然后使用OuterHtml获取html内容:

HtmlNode divContainer = htmlDocument.DocumentNode.SelectSingleNode("//div[@id='singlePlay']");

HtmlNode iframe = divContainer.SelectSingleNode("//iframe");

Console.WriteLine(iframe.OuterHtml);