VB.NET& HtmlAgilityPack解析元素属性

时间:2014-08-15 12:35:49

标签: vb.net parsing html-agility-pack

我有像这样的HTML文件

<html>
      <head>

    <title>Page Name in a Folder</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <link href="resources/css/jquery-ui-themes.css" type="text/css" rel="stylesheet"/>
    <link href="resources/css/axure_rp_page.css" type="text/css" rel="stylesheet"/>
    <link href="data/styles.css" type="text/css" rel="stylesheet"/>
    <link href="files/page_name_in_a_folder/styles.css" type="text/css" rel="stylesheet"/>

  </head>
  <body>
    <div id="base" class="">

      <!-- Image Shape Name (Image) -->
      <div id="u0" class="ax_image" data-label="Image Shape Name">
        <img id="u0_img" class="img " src="images/page_name_not_in_a_folder/u0.png"/>
        <!-- Unnamed () -->
        <div id="u1" class="text">
          <p><span>&nbsp;</span></p>
        </div>
      </div>

      <!-- Heading 1 Shape Name (Shape) -->
      <div id="u2" class="ax_h1" data-label="Heading 1 Shape Name">
        <img id="u2_img" class="img " src="resources/images/transparent.gif"/>
        <!-- Unnamed () -->
        <div id="u3" class="text">
          <p><span>Heading 1</span></p>
        </div>
      </div>

      <!-- Heading 2 Shape Name (Shape) -->
      <div id="u4" class="ax_h2" data-label="Heading 2 Shape Name">
        <img id="u4_img" class="img " src="resources/images/transparent.gif"/>
        <!-- Unnamed () -->
        <div id="u5" class="text">
          <p><span>Heading 2</span></p>
        </div>
      </div>

      <!-- Label Shape Name (Shape) -->
      <div id="u6" class="ax_paragraph" data-label="Label Shape Name">
        <img id="u6_img" class="img " src="resources/images/transparent.gif"/>
        <!-- Unnamed () -->
        <div id="u7" class="text">
          <p><span>Label</span></p>
        </div>
      </div>



      <!-- Unnamed (HTML Button) -->
      <div id="u26" class="ax_html_button">
        <input id="u26_input" type="submit" value="Submit"/>
      </div>
    </div>
  </body>
</html>

我需要提取所有DIV及其类和属性,例如:

  • 班级名称:(ax_html_button)按钮的提取值=“提交”
  • 类名:(ax_paragraph)数据标签的提取值=“标签 形状名称“

尝试使用HtmlAgilityPack:

Public Shared Sub parseAgility(fName As String)
        Dim htmlDoc As New HtmlAgilityPack.HtmlDocument()
        htmlDoc.OptionFixNestedTags = True

        htmlDoc.Load(fName)

        Dim classes As New List(Of String)()
        For Each node As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//body//div")

            classes.Add(node.InnerHtml)
       Next
    End Sub

但不确定如何处理所有属性。 有什么想法吗?

以及如何获取input元素的值(&#34;提交&#34;)?

 <div id="u26" class="ax_html_button">
    <input id="u26_input" type="submit" value="Submit"/>
  </div>

如果我输入这个,我得到&#34; u16_input&#34;元素而不是&#34; u26&#34; !?

For Each node As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//body//div")
            Dim className = node.GetAttributeValue("class", "")
            Select Case className
                Case "ax_html_button"
                    Dim node2 As HtmlNode = node.SelectSingleNode("//input")

                    value= node2.GetAttributeValue("value", "")
                Case "ax_paragraph"
                Case "ax_h1"
                Case "ax_h2"
                Case "ax_h3"
                Case "ax_h4"
                Case "ax_h5"
                Case "ax_h6"
                Case "ax_checkbox"
            End Select
        Next
编辑:找到解决方案。

1 个答案:

答案 0 :(得分:0)

我们并不完全清楚你想要得到什么。但是这里有一个从类属性中获取类名的示例:

For Each node As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//body//div")
    Dim className = node.GetAttributeValue("class", "");
    If Not String.IsNullOrEmpty(className) Then classes.Add(className)
Next

您可以使用类似方法获取<div>的其他属性。