【问题标题】:strip out everything out side of <img src=random.jpg> and <p>random text</p> in html去掉 html 中 <img src=random.jpg> 和 <p>random text</p> 之外的所有内容
【发布时间】:2011-11-30 04:00:13
【问题描述】:

我正在尝试使用 c# http 模块从网页中删除数据。我只想要原始文本和图像。我怎样才能把其他所有东西都去掉?

private static Regex reg = new Regex(@"<img src=\t????????");

public override void Write(byte[] buffer, int offset, int count)
    {
      byte[] data = new byte[count];
      Buffer.BlockCopy(buffer, offset, data, 0, count);
      string html = System.Text.Encoding.Default.GetString(buffer);

      html = reg.Replace(html, string.Empty);


      byte[] outdata = System.Text.Encoding.Default.GetBytes(html);
      _sink.Write(outdata, 0, outdata.GetLength(0));
    }

【问题讨论】:

  • @Blender - HTML 解析器会是更好的选择。
  • 是的,我知道这不是完全重复的,但接受的答案仍然回答这个:stackoverflow.com/questions/1732348/…
  • 该线程被垃圾邮件发送到地狱。任何其他解决方案,例如使用哪个 XMl 解析器???
  • @user719825 - 评论不是答案。

标签: c# asp.net html iis httpmodule


【解决方案1】:

使用 HTML 解析器,例如 HtmlAgilityPack

【讨论】:

  • 我现在正在使用它,但我只能通过实际页面加载它。我将如何获取缓冲区中的数据(在页面呈现在浏览器上之前?)
  • 有没有可以覆盖的Render 方法? (不记得这是否仅适用于Pages)。
猜你喜欢
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 2023-03-17
  • 2012-09-05
  • 2010-09-06
  • 1970-01-01
  • 2022-09-25
  • 2019-10-18
相关资源
最近更新 更多