【发布时间】:2026-01-26 00:45:01
【问题描述】:
我尝试使用 HTML 敏捷包解析以下 HTML。
这是代码返回的整个文件的 sn-p:
<div class="story-body fnt-13 p20-b user-gen">
<p>text here text here text </p>
<p>text here text here text text here text here text text here text here text text here text here text </p>
<div class="gallery clr bdr aln-c js-no-shadow mod cld">
<div>
<ol>
<li class="fader-item aln-c ">
<div class="imageWrap m10-b">
​<img class="http://www.domain.com/picture.png| " src="http://www.domain.com/picture.png" alt="alt text" />
</div>
<p class="caption">caption text</p>
</li>
</ol>
</div>
</div >
<p>text here text here text text here text here text text here text here text text here text here text </p>
<p>text here text here text text here text here text text here text here text text here text here text text here text here text </p>
<p>text here text here text text here text here text text here text here text text here text here text text here text here text </p>
</div>
我使用以下代码获得了这个 sn-p 代码(我知道这很混乱)
string url = "http://www.domain.com/story.html";
var webGet = new HtmlWeb();
var document = webGet.Load(url);
var links = document.DocumentNode
.Descendants("div")
.Where(div => div.GetAttributeValue("class", "").Contains("story-body fnt-13 p20-b user-gen")) //
.SelectMany(div => div.Descendants("p"))
.ToList();
int cn = links.Count;
HtmlAgilityPack.HtmlNodeCollection tl = document.DocumentNode.SelectNodes("/html[1]/body[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/div[1]");
foreach (HtmlAgilityPack.HtmlNode node in tl)
{
textBox1.AppendText(node.InnerText.Trim());
textBox1.AppendText(System.Environment.NewLine);
}
代码循环遍历每个p 并(目前)将其附加到文本框。除了类gallery clr bdr aln-c js-no-shadow mod cld 的div 标记之外,所有都正常工作。这段 HTML 的结果是我得到了 &#8203; 和标题文本位。
从结果中省略它的最佳方法是什么?
【问题讨论】:
-
嘘...
So two questions, what's the best way to omit that from the results?这是一个问题,另一个是什么? -
我不知道你在说什么......:p
标签: c# html-agility-pack