Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Go解析HTML文件_Html_Xml_Go - Fatal编程技术网

用Go解析HTML文件

用Go解析HTML文件,html,xml,go,Html,Xml,Go,encoding/xml是解析像这样的HTML表文件的最好的库吗?有一些例子如何做到这一点 <html><head> <meta charset="utf-8"> </head> <body> <a name="Test1"> <center> <b>Test 1</b> <table border="0"> <tbody><tr> <t

encoding/xml是解析像这样的HTML表文件的最好的库吗?有一些例子如何做到这一点

<html><head>
<meta charset="utf-8">

</head>
<body>
<a name="Test1">
<center>
<b>Test 1</b> <table border="0">
  <tbody><tr>
  <th> Type </th>
  <th> Region </th>
  </tr>
  <tr>
  <td> <table border="0">
  <thead>
  <tr>
    <th><b>Type</b></th>
    <th> &nbsp; </th>
    <th> Count </th>
    <th> Percent </th>
  </tr>
  </thead>
  <tbody><tr>
    <td> <b>T1</b> </td>
    <th> &nbsp; </th>
    <td class="numeric" bgcolor="#ff0000"> 34,314 </td>
    <td class="numeric" bgcolor="#ff0000"> 31.648% </td>
  </tr>
  <tr>
    <td> <b>T2</b> </td>
    <th> &nbsp; </th>
    <td class="numeric" bgcolor="#bf3f00"> 25,820 </td>
    <td class="numeric" bgcolor="#bf3f00"> 23.814% </td>
  </tr>
  <tr>
    <td> <b>T3</b> </td>
    <th> &nbsp; </th>
    <td class="numeric" bgcolor="#24da00"> 4,871 </td>
    <td class="numeric" bgcolor="#24da00"> 4.493% </td>
  </tr>

</tbody></table><br>
</td>
  <td> <table border="0">
  <thead>
  <tr>
    <th><b> Type</b></th>
    <th> &nbsp; </th>
    <th> Count </th>
    <th> Percent </th>
  </tr>
  </thead>
  <tbody><tr>
    <td> <b>T4</b> </td>
    <th> &nbsp; </th>
    <td class="numeric" bgcolor="#ff0000"> 34,314 </td>
    <td class="numeric" bgcolor="#ff0000"> 31.648% </td>
  </tr>
  <tr>
    <td> <b>T5</b> </td>
    <th> &nbsp; </th>
    <td class="numeric" bgcolor="#53ab00"> 11,187 </td>
    <td class="numeric" bgcolor="#53ab00"> 10.318% </td>
  </tr>
  <tr>
    <td> <b>T6</b> </td>
    <th> &nbsp; </th>
    <td class="numeric" bgcolor="#bf3f00"> 25,820 </td>
    <td class="numeric" bgcolor="#bf3f00"> 23.814% </td>
  </tr>

</tbody></table><br>
</td>
  </tr>
</tbody></table>
</center>

  </a>
</body></html>

测试1
类型
区域
类型
计数
百分比
T1
34,314 
31.648% 
T2
25,820 
23.814% 
T3
4,871 
4.493% 

类型 计数 百分比 T4 34,314 31.648% T5 11,187 10.318% T6 25,820 23.814%
提前谢谢。

取决于您的HTML

严格地说,唯一一种保证由一致的XML解析器解析的HTML是XHTML,但尽管XHTML曾经被认为是HTML标准,但它并没有真正起步,现在它被认为是过时的(支持大肆宣传的“HTML5”)以及它周围的所有生态系统)。HTML的基本问题是,虽然它看起来像XML,但它有不同的规则。一个明显的区别是

是一个完全合法的HTML,但在XML中是一个未终止的元素(在后者中,它必须拼写为

),并且

另一方面,在我看来,您的特定示例非常XML化,因此如果您可以保证您的数据在成为HTML的同时始终是一个XML,那么您可以使用
编码/XML
包。否则,按照@elithrar的建议,选择或查找其他包。

取决于您的HTML

严格地说,唯一一种保证由一致的XML解析器解析的HTML是XHTML,但尽管XHTML曾经被认为是HTML标准,但它并没有真正起步,现在它被认为是过时的(支持大肆宣传的“HTML5”)以及它周围的所有生态系统)。HTML的基本问题是,虽然它看起来像XML,但它有不同的规则。一个明显的区别是

是一个完全合法的HTML,但在XML中是一个未终止的元素(在后者中,它必须拼写为

),并且

另一方面,在我看来,您的特定示例非常XML化,因此如果您可以保证您的数据在成为HTML的同时始终是一个XML,那么您可以使用
编码/XML
包。否则,按照@elithrar的建议,去搜索,或者找到其他软件包。

您尝试过吗?第一个谷歌搜索结果是“golang html解析器”;)你们有并没有试过——第一个谷歌搜索“golang html解析器”的结果