2017-05-31 1 views
1

je une ligne qui fait partie du html renvoyé:Extraction <span /> partie du noeud de html

<h1 id="pgName" class="floatLVal tight">IBM Dividend Yield (TTM):</h1><span id="pgNameVal">3.74% for May 31, 2017</span> 

que je lise comme suit. Je vois le nœud, mais je ne peux pas voir aucun texte passé (TTM): dans l'un des champs singleNode, par exemple, dans le cadre de innerhtml. J'aimerais extraire la partie «3,74% pour le 31 mai 2017».

Quelle est la bonne façon d'extraire cette information?

HtmlDocument document = ... 
var singleNode = document.DocumentNode.SelectSingleNode("//h1[@class='floatLVal tight']"); 

Répondre

1

tag span ne se trouve pas dans h1 balise. Soit obtenir le noeud suivant frère de h1

var h1 = document.DocumentNode.SelectSingleNode("//h1[@class='floatLVal tight']"); 
var span = h1.NextSibling; 
var text = span.InnerHtml; // "3.74% for May 31, 2017" 

Ou procurez-vous span noeud par ID:

var span = document.DocumentNode.SelectSingleNode("//span[@id='pgNameVal']"); 
var text = span.InnerHtml; // "3.74% for May 31, 2017" 
+1

Merci qui fonctionne. – Ivan