简体   繁体   中英

c#, html-agility-pack get text which is not inside tags

This is my HTML:

<a class="bla"></a>
25 oct 2012

How can I get only 25 oct 2012. The text is not inside any tags. I am using the c# htmlagilitypack library.

Basically, you can use text() to reference text nodes in XPath. Try to pass the following XPath to HtmlAgilityPack's SelectNodes() or SelectSingleNode() method :

//a[@class='bla']/following-sibling::text()[1]

brief explanation :

  • //a[@class='bla'] : find <a> element, anywhere in the HTML document, that have class attribute equals "bla" ...
  • /following-sibling::text()[1] : then from such <a> return the nearest text node that follows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM