0

我试图使用 HTMLAgilityPack 来操作一些 Html。我也决定尝试 CSQuery。

目标是提取和 img 标签,然后 src 并将其重新插入到 h3 标签的前面。

假设 html:

 <div class="col-md-6">
            <div class="item">
                <div class="content galleryItem">
                    <h3>
                        Al Shabaab kill at least 29 in latest attacks on Kenyan coast                            
                    </h3>           
                   <p> <img alt src="../../../../images/AlShabaab.jpg"></p>
<p>
    Al Shabaab killed at least 29 people in two coastal areas of Kenya.</p>

                </div>
            </div>
        </div>

目标是将img移到h3前面

我使用以下内容从 img 标签中删除样式 attr:

   Dim csq = CQ.Create(input)
   Dim csstyle = csq("img")
   Return csstyle.RemoveAttr("style")
4

1 回答 1

1

由于您没有明确标记此 VB.NET,我将用 C# 回答,我希望没关系:

var cq = CQ.create(input); // create the CsQuery source
var img = cq["img"]; // image here, img["src"] is its source
img.Remove().InsertBefore(cq["h3"]);// remove it, and add it in front of H3.

当然,这段代码可以更短,但我希望代码与您的文字描述相匹配。

于 2014-07-06T21:43:45.937 回答