【问题标题】:How to extract image from HTML and move in front of heading tag?如何从 HTML 中提取图像并移动到标题标签的前面?
【发布时间】:2014-07-06 13:35:09
【问题描述】:

我对这个有点难过。我有一些 html,其中包含图像,然后是一些文本。但我需要重新排列 html,以便首先显示图像 - 所以图像,然后是 h3 标记,然后是文本。

编辑:下面的代码实际上并没有删除样式属性。我认为它正在工作,直到我更仔细地查看了 html 源代码。所以我需要帮助来剥离给定的样式属性

<p>
<img alt="" src="../../../../images/PeterDoocy5.jpg" style="width: 608px; height: 316px;" /></p>

到目前为止,我已经设法使用 HAP 去除页面中图像的样式属性:

 <Extension()> Public Function RemoveStyleAttributes(input As String)
        Dim cleint As New WebClient

        Dim html As New HtmlDocument
        html.LoadHtml(input)

        Dim elementsWithStyleAttribute = html.DocumentNode.SelectNodes("//@img")

        If elementsWithStyleAttribute IsNot Nothing Then
            For Each element In elementsWithStyleAttribute
                element.Attributes("style").Remove()
            Next
        End If
        Return input
    End Function

但是我不知道如何将图像拉到 H3 标签的前面。

HTML:

<div class="col-md-6">
   <div class="item">
      <div class="content galleryItem">
         <h3>
            DOJ court docs in Abu Khattallah case dispel Obama Admin narrative about the anti-Islam video                            
         </h3>
         <p>
            <img alt="" class="img-responsive" src="../../../../images/AbuKhattala.jpg" />
         </p>
         <p>
            But it was an awful, disgusting video.....
         </p>
      </div>
   </div>
</div>

现在的扩展方法:

   <Extension()> Public Function RemoveStyleAttributes(html As HtmlDocument)


        Dim divs = html.DocumentNode.SelectNodes("//div[@class='content galleryItem']")

        For Each div As HtmlNode In divs
            'get <img> and remove its style attribute'
            Dim img = div.SelectSingleNode("./p/img[@style]")
            img.Attributes("style").Remove()
            'remove <h3> and <p>text here</p>'
            Dim h3 = div.SelectSingleNode("./h3")
            h3.Remove()
            Dim text = div.SelectSingleNode("./p[not(img)]")
            text.Remove()
            'add <h3> and <p>text here</p> to the parent again in desired order'
            div.AppendChild(h3)
            div.AppendChild(text)
        Next


        Return html.DocumentNode.OuterHtml.ToString
    End Function

尝试将其用作@Html.Raw(item.PostSummary.RemoveStyleAttributes)

【问题讨论】:

    标签: html asp.net-mvc vb.net html-agility-pack


    【解决方案1】:

    你可以试试这个方法:

    <Extension()> Public Function RemoveStyleAttributes(input As String)
        Dim cleint As New WebClient
        Dim html As New HtmlDocument
        html.LoadHtml(input)
    
        For Each div As HtmlNode In divs
            'get <img> and remove its style attribute'
            Dim img = div.SelectSingleNode("./p/img[@style]")
            img.Attributes("style").Remove()
            'remove <h3> and <p>text here</p>'
            Dim h3 = div.SelectSingleNode("./h3")
            h3.Remove()
            Dim text = div.SelectSingleNode("./p[not(img)]")
            text.Remove()
            'add <h3> and <p>text here</p> to the parent again in desired order'
            div.AppendChild(h3)
            div.AppendChild(text)
        Next
        Return html.DocumentNode.OuterHtml.ToString
    End Function
    

    输出(格式化。给定输入 html,如本问题中所述)

    <div class="col-md-6">
       <div class="item">
          <div class="content galleryItem">
             <p>
                <img alt="" class="img-responsive" src="../../../../images/AbuKhatta
                   la.jpg">
             </p>
             <h3>
                DOJ court docs in Abu Khattallah case dispel Obama Admin narrative a
                bout the anti-Islam video
             </h3>
             <p>
                But it was an awful, disgusting video.....
             </p>
          </div>
       </div>
    </div>
    

    【讨论】:

    • 我尝试将其作为扩展方法并获取未将对象引用设置为对象实例。我的观点是我尝试了@Html.Raw(item.PostSummary.RemoveStyleAttributes) 但这没有用。就用作扩展方法而言,我缺少什么?
    • 你的扩展方法应该返回html.DocumentNode.OuterElement,而不是原始的未处理input...
    • 如果我返回 html.documentnode.outerhtml,它不会通过智能感知显示为扩展方法。我在扩展方法的原始答案中发布了代码。
    • 不要改变函数参数,只改变返回值。检查我更新的代码...
    • 好的,刚刚又试了一次,我仍然得到一个对象引用错误。这就是我调用扩展方法的方式:@Html.Raw(item.PostSummary.RemoveStyleAttributes)
    猜你喜欢
    • 2010-10-17
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多