【问题标题】:how to save the text in the web page in readable format如何以可读格式保存网页中的文本
【发布时间】:2011-09-21 00:01:50
【问题描述】:

例如,我有一个网页 (http://guyism.com/humor/mathematical-equations-that-explain-men.html)。使用HttpWebRequest我可以得到网页的流。 现在我想将内容(见图)显示回浏览器,如下图所示。怎么能做到这一点。

【问题讨论】:

  • 这是您服务器上的一个页面,还是您试图显示您无法控制的内容?它可以使方法有所不同。
  • 您能展示一下您使用 HttpWebRequest 获取远程页面流的方式吗?你想简单地在页面的 div 中呈现该内容吗?
  • 另外,您尝试访问的页面是否...是否需要登录?
  • 一个 div 不起作用,因为他需要链接文件来加载,比如 css、javascript 等......另外,他想要一个图像格式,这意味着需要一些翻译/渲染在某个时候完成。
  • 我无法控制内容。可能是随机页面。

标签: c# .net asp.net html


【解决方案1】:

使用 iframe 可能是解决问题的一种方法,具体取决于您要查找的内容。如果您想抓取页面并将它们吐出,另一种方法是使用 cURL。它还可以提供类似的功能。有一些基于它的网络代理。以下是两者的一些信息。

iframe:

http://www.iframehtml.com/

卷曲: http://curl.haxx.se/libcurl/c/example.html

.Net 的 cURL http://curl.haxx.se/libcurl/dotnet/

【讨论】:

    【解决方案2】:

    怎么样,使用 WebBrowser 控件。但是,这取决于 guyism.com 的结构,因此对于随机页面,您可以查找容器 div (??)

            void Load()
            {
                string html = DownloadHtml( "http://guyism.com/humor/mathematical-equations-that-explain-men.html" );
    
                MatchCollection matches = Regex.Matches( html, @"(<div class=""blog-post-inside"">.*?<center>---</center>)", RegexOptions.Singleline );    
    
                webBrowser.ScriptErrorsSuppressed = true;
    
                webBrowser.DocumentText = matches[ 0 ].Groups[ 1 ].Value;
            }
    
            string DownloadHtml( string url )
            {
                using ( var client = new WebClient() )
                {
                    using ( var reader = new StreamReader( client.OpenRead( url ) ) )
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2011-04-06
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 2021-02-11
      • 1970-01-01
      相关资源
      最近更新 更多