C# 如何在windows上使用lynx.exe将html字符串(带有css和格式错误的html)转换为纯文本

C# 如何在windows上使用lynx.exe将html字符串(带有css和格式错误的html)转换为纯文本,c#,.net,html,vb.net,lynx,C#,.net,Html,Vb.net,Lynx,我已经尝试过使用正则表达式来完成这项任务(),因此我尝试过使用HTMLAgilityPack,但结果文本非常糟糕,html列表()完全丢失,只会导致段落聚集在一起 在这个问题上,我看到lynx(为windows编译)被推荐为一个好的替代方案,但是我很难做到这一点——如何使用lynx.exe将html(存储在.net字符串中)转换为带换行符的可呈现的纯文本字符串等 我唯一能想到的方法是将html写入一个文件,使用.nets system.process调用lynx.exe-dump并读取生成的文件

我已经尝试过使用正则表达式来完成这项任务(),因此我尝试过使用HTMLAgilityPack,但结果文本非常糟糕,html列表(
  • )完全丢失,只会导致段落聚集在一起

    在这个问题上,我看到lynx(为windows编译)被推荐为一个好的替代方案,但是我很难做到这一点——如何使用lynx.exe将html(存储在.net字符串中)转换为带换行符的可呈现的纯文本字符串等

    我唯一能想到的方法是将html写入一个文件,使用.nets system.process调用lynx.exe-dump并读取生成的文件——这看起来非常笨拙

    有更好的方法吗? 对于这样的任务,确切的lynx.exe命令行是什么

    我使用的LYNX实现如下:

    编辑:取得了一些进展,这是我一直使用的命令行:

    lynx.exe -dump "d:\test.html" >d:\output.txt
    
    它可以正常工作,但如果我在记事本中打开生成的文件,它将全部放在一行上(因为lynx只对新行使用换行符,而记事本需要回车才能正确渲染)

    此外,它在
  • &

    标记后插入太多换行符,这两个换行符是:

       Hello, this is a normal line of text.
    
    
       Next an ordered list:
    
    
       1.       The
    
       2.       Quick
    
       3.       Brown Fox
    
       4.       Jumped
    
    我可以通过用一个LF替换两个连续的LF来解决这个问题,但我仍然需要一个c#包装器

    编辑2-我根据Christian的答案得出的最终解决方案:

    Function ConvertHtmlToPlainText(ByVal HtmlString As String) As String
    
        '#### Define FileBuffer Path
        Dim HtmlBuffer As String = WorkingRoot & "HtmlBuffer.html"
    
        '#### Delete any old buffer files
        Try
            If File.Exists(HtmlBuffer) = True Then
                File.Delete(HtmlBuffer)
            End If
        Catch ex As Exception
            Return "Error: Deleting old buffer file: " & ex.Message
        End Try
    
        '#### Write the HTML to the buffer file
        Try
            File.WriteAllText(WorkingRoot & "HtmlBuffer.html", HtmlString)
        Catch ex As Exception
            Return "Error: Writing new buffer file: " & ex.Message
        End Try
    
        '#### Check the file was written OK
        If File.Exists(HtmlBuffer) = False Then
            Return "Error: HTML Buffer file was not written successfully."
        End If
    
        '#### Read the buffer file with Lynx and capture plain text output
        Try
            Dim p = New Process()
            p.StartInfo = New ProcessStartInfo(LynxPath, "-dump -width 1000 " & HtmlBuffer)
            p.StartInfo.WorkingDirectory = WorkingRoot
            p.StartInfo.UseShellExecute = False
            p.StartInfo.RedirectStandardOutput = True
            p.StartInfo.RedirectStandardError = True
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
            p.StartInfo.CreateNoWindow = True
            p.Start()
            p.WaitForExit()
    
            '#### Grab the text rendered by Lynx
            Dim text As String = p.StandardOutput.ReadToEnd()
            Return text.Replace(vbLf & vbLf, vbLf)
    
        Catch ex As Exception
            Return "Error: Error running LYNX to parse the buffer: " & ex.Message
        End Try
    End Function
    

    使用此功能,您可以调用Lynx,将重定向的StandardOutput的输出捕获为字符串,而无需先将其写入文件

    using System;
    using System.Diagnostics;
    
    namespace Lynx.Dumper
    {
      public class Dampler
      {
          public void fdksfjh()
          {
              var url = "http://www.google.com";
    
              var p = new Process();
    
              p.StartInfo = new ProcessStartInfo("c:/tools/lynx_w32/lynx.exe", "-dump -nolist " + url)
              {
                  WorkingDirectory = "c:/tools/lynx_w32/",
                  UseShellExecute = false,
                  RedirectStandardOutput = true,
                  RedirectStandardError = true,
                  WindowStyle = ProcessWindowStyle.Hidden,
                  CreateNoWindow = true
              };
    
              p.Start();
              p.WaitForExit();
    
              //grab the text rendered by Lynx
              var text = p.StandardOutput.ReadToEnd();
    
              Console.WriteLine(text);
          }
      }
    }
    
    非常好的解决方案,干杯(关于我的最终解决方案,请参见上面问题的编辑2)