请检查下面的代码。我正在尝试从此html文档中获取html文本值。我想获取文本Quick Kill 32 oz. Mosquito Yard Spray
,并且我已经尝试使用SelectSingleNode
来实现,例如波纹管,但这无法获取此文本值。知道如何解决吗?
string html = @"<div class='pod-plp__description js-podclick-analytics' data-podaction='product name'>
<a class='' data-pos='0' data-request-type='sr' data-pod-type='pr' href='/p/AMDRO-Quick-Kill-32-oz-Mosquito-Yard-Spray-100530440/304755303'>
<span class='pod-plp__brand-name'>AMDRO</span>
Quick Kill 32 oz. Mosquito Yard Spray
</a>
</div>";
var doc = new HtmlDocument();
doc.Load(html);
string title = doc.DocumentNode
.SelectSingleNode("//div[@class='pod-plp__description js-podclick-analytics']span[@class='pod-plp__brand-name']")
.InnerText;
答案 0 :(得分:1)
您正试图仅定位span[@class='pod-plp__brand-name']
,这只会使您在跨度范围内返回,但是您需要following-sibling::text()
才能在跨度之后获取文本。请参见下面的示例代码。您也可以从html-agility-pack官方网站上了解更多信息。
var Content = htmlDoc.DocumentNode.SelectSingleNode("//span[@class='pod-plp__brand-name']/following-sibling::text()[1]");
string title = titleAgain.InnerText.Trim();
找到的解决方案from here