0

以下是我的 html 片段,有 N 个带有类名的 DIVquality现在我想提取<a href>每个具有质量的类名的 div。

Eastin Easy Citizen 艾哈迈达巴德

<div class="quality wrap">
<a href="/Hotel_Review-g297608-d4464287-Reviews-Eastin_Easy_Citizen_Ahmedabad-Ahmedabad_Gujarat.html" id="property_4464287"class="property_title" onclick=" ta.setEvtCookie('Reviews', 'HotelName', 297608, 0, this.href); ta.util.cookie.setPIDCookie(15176);">
Eastin Easy Citizen Ahmedabad</a> </div>

<div class="quality wrap">
<a href="/Hotel_Review-g297608-d4464287-Reviews-Eastin_Easy_Citizen_Ahmedabad-Ahmedabad_Gujarat.html" id="property_4464287"class="property_title" onclick=" ta.setEvtCookie('Reviews', 'HotelName', 297608, 0, this.href); ta.util.cookie.setPIDCookie(15176);">
Eastin Easy Citizen Ahmedabad</a> </div>

<div class="quality wrap">
<a href="/Hotel_Review-g297608-d4464287-Reviews-Eastin_Easy_Citizen_Ahmedabad-Ahmedabad_Gujarat.html" id="property_4464287"class="property_title" onclick=" ta.setEvtCookie('Reviews', 'HotelName', 297608, 0, this.href); ta.util.cookie.setPIDCookie(15176);">
Eastin Easy Citizen Ahmedabad</a> </div>

我试过以下

var nS = page.DocumentNode.SelectNodes("//div[@class='quality']//a");
            foreach (HtmlNode linkNode in nS)
            {
                //do something
            }

但我得到NullPointerException任何人都可以帮助我

4

1 回答 1

1

它应该是质量包装而不是质量

"//div[@class='quality wrap']//a"

所以它会是

var hrefList=page.DocumentNode
                 .SelectNodes("//div[@class='quality wrap']//a")
                 .Where(e=>e.InnerText.Trim()=="Eastin Easy Citizen Ahmedabad")
                 .Select(x=>x.Attributes["href"].Value)
                 .ToList();
于 2013-09-30T07:04:29.753 回答