2017-09-04 4 views
2

に基づいて、私はこのように2つのHTML要素のブロックを持っている:はHtmlAgilityPackフィルタリングHTMLクエリ

<div class="a-row"> 
    <a class="a-size-small a-link-normal a-text-normal" href="/Chemical-Guys-CWS-107-Extreme-Synthetic/dp/B003U4P3U0/ref=sr_1_1_sns?s=automotive&amp;ie=UTF8&amp;qid=1504525216&amp;sr=1-1"> 
     <span aria-label="$19.51" class="a-color-base sx-zero-spacing"> 
      <span class="sx-price sx-price-large"> 
       <sup class="sx-price-currency">$</sup> 
       <span class="sx-price-whole">19</span> 
       <sup class="sx-price-fractional">51</sup> 
      </span> 
     </span> 
     <span class="a-letter-space"></span>Subscribe &amp; Save 
    </a> 
</div> 

そして、HTMLの次のブロック:これらの要素の

<div class="a-row a-spacing-none"> 
    <a class="a-link-normal a-text-normal" href="https://rads.stackoverflow.com/amzn/click/B003U4P3U0"> 
     <span aria-label="$22.95" class="a-color-base sx-zero-spacing"> 
      <span class="sx-price sx-price-large"> 
       <sup class="sx-price-currency">$</sup> 
       <span class="sx-price-whole">22</span> 
       <sup class="sx-price-fractional">95</sup> 
      </span> 
     </span> 
    </a> 
    <span class="a-letter-space"></span> 
    <i class="a-icon a-icon-prime a-icon-small s-align-text-bottom" aria-label="Prime"> 
     <span class="a-icon-alt">Prime</span> 
    </i> 
</div> 

はどちらも非常によく似ていますその構造では、トリックは、私はそれの横にあるクラスのスパン要素を含む要素の値を抽出する:aria-label = "Prime"

これは私が現在価格bを抽出する方法です

if (htmlDoc.DocumentNode.SelectNodes("//span[@class='a-color-base sx-zero-spacing']") != null) 
{ 
    var span = htmlDoc.DocumentNode.SelectSingleNode("//span[@class='a-color-base sx-zero-spacing']"); 
    price = span.Attributes["aria-label"].Value; 
} 

これは基本的に、複数の要素が存在するため、位置0のHTML要素を選択します。しかしここでのトリックは、私が示したHTMLの2番目のピースのように、素数を含むspan要素を選択したいということです。 このような値を持つ2番目の要素が存在しない場合は、単に私がそこに書いたこの最初の方法を使用してください...

誰かがこれで私を助けることができますか? =)

私もこのような何か試してみた:

をしかし、それはまだ最初の要素を返しますxDで

新バージョンの連中:

var pr = htmlDoc.DocumentNode.SelectNodes("//a[@class='a-link-normal a-text-normal']"); 
string prrrrrr = ""; 
for (int i = 0; i < pr.Count; i++) 
    { 
    if (pr.ElementAt(i).SelectNodes("//i[@class='a-icon a-icon-prime a-icon-small s-align-text-bottom']").ElementAt(i) != null) 
    { 
    prrrrrr = pr.ElementAt(i).SelectNodes("//span[@class='a-color-base sx-zero-spacing']").ElementAt(i).Attributes["aria-label"].Value; 

    } 
} 

だから、アイデアは私ということですHTMLファイルからすべての "a"要素を取り出し、aのHTML Nodeコレクションを作成し、それらをループして、探している要素が実際に含まれているかどうかを確認してから一致させます。ここ

問題は、この文は常に合格した場合ということです:

if (pr.ElementAt(i).SelectNodes("//i[@class='a-icon a-icon-prime a-icon-small s-align-text-bottom']").ElementAt(i) != null) 

どのようにできたノードコレクション内の個々の素子を介してIループ?

+0

? =) – User987

+0

誰でもみんなにコードの別の部分を追加しました – User987

答えて

1

divのレベルをクラスa-rowで調べる必要があると思います。次に、divに「area-label」が「Prime」と等しいiが含まれているかどうかをループしてチェックします。そして最後にa-color-base sx-zero-spacingクラスとspanと、このような属性aria-labelの値を取得する:

HtmlNodeCollection nodes = htmlDoc.DocumentNode.SelectNodes("//div[starts-with(@class,'a-row')]"); 

foreach (HtmlNode node in nodes) 
{ 
    HtmlNode i = node.SelectSingleNode("i[@aria-label='Prime']"); 

    if (i != null) 
    { 
     HtmlNode span = node.SelectSingleNode(".//span[@class='a-color-base sx-zero-spacing']"); 

     if (span != null) 
     { 
      string currentValue = span.Attributes["aria-label"].Value; 
     } 
    } 
} 
@Stephen Mueckeがちょうど質問= D
関連する問題