c# 使用 htmlagilitypack 获取 iframe kod?
c# get iframe kod with htmlagilitypack?
我正在使用 C# 和 HtmlAgilityPack,我想从网站的 html.
获取 iframe kod
这是 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>
最后,我想从输出中排除 "p" 标签,请帮助我。
您应该能够从 divContainer 节点获取 iframe,然后使用 OuterHtml
获取 html 内容:
HtmlNode divContainer = htmlDocument.DocumentNode.SelectSingleNode("//div[@id='singlePlay']");
HtmlNode iframe = divContainer.SelectSingleNode("//iframe");
Console.WriteLine(iframe.OuterHtml);
我正在使用 C# 和 HtmlAgilityPack,我想从网站的 html.
获取 iframe kod这是 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>
最后,我想从输出中排除 "p" 标签,请帮助我。
您应该能够从 divContainer 节点获取 iframe,然后使用 OuterHtml
获取 html 内容:
HtmlNode divContainer = htmlDocument.DocumentNode.SelectSingleNode("//div[@id='singlePlay']");
HtmlNode iframe = divContainer.SelectSingleNode("//iframe");
Console.WriteLine(iframe.OuterHtml);