小编Rub*_*yan的帖子

HTML XPath按类名搜索

我在c#中的xpath有问题,
我想找到具有这种结构的所有元素,
我有10个链接,所有链接都具有这种结构:

<div class="PartialSearchResults-item" data-zen="true">
<div class="PartialSearchResults-item-title">
<a class="PartialSearchResults-item-title-link result-link"target="_blank" href='https://www.google.com/'> Google</a>
    </div>
<p class="PartialSearchResults-item-url">www.google.com</p>
<p class="PartialSearchResults-item-abstract">Search the world.</p>
   </div>
Run Code Online (Sandbox Code Playgroud)

例如,通过此示例,我希望获得“ Google”和“ www.google.com”以及“搜索世界”。

var titles = hd.DocumentNode.SelectNodes("//div[contains(@class, 'PartialSearchResults-item')]");
string link;
foreach (HtmlNode node in titles){
string description = node.SelectSingleNode(".//*[contains(@class,'PartialSearchResults-item-abstract')]").InnerText;

link = node.SelectSingleNode(".//*[contains(@class,'PartialSearchResults-item-url')]").InnerText;

 string title = node.SelectSingleNode(".//a[contains(@class,'PartialSearchResults-item-title-link result-link')]").InnerText;}
Run Code Online (Sandbox Code Playgroud)

但是我得到错误的空引用

html c# asp.net xpath html-agility-pack

2
推荐指数
1
解决办法
3296
查看次数

标签 统计

asp.net ×1

c# ×1

html ×1

html-agility-pack ×1

xpath ×1