Html Agility Pack - Problem selecting subnode

前端 未结 2 1695
灰色年华
灰色年华 2020-12-08 00:55

I want to export my Asics running plan to iCal and since Asics do not offer this service, I decided to build a little scraper for my own personal use. What I want to do is t

相关标签:
2条回答
  • 2020-12-08 01:37

    In XPATH, // means all children and grand children below the current node. So you need to come up with a more restrictive XPATH expression. If you provide the real HTML, and what you're looking for exactly, we can help you dig further.

    About the error you have:

    .div[@class='date'] is invalid because . is sticked to div. You could use div[@class='date'], or ./div[@class='date'] which I believe are equivalent. This is because . is an XPATH axe, which is an alias for self and means "the current node".

    0 讨论(0)
  • 2020-12-08 01:43

    A few things that will help you when working with HtmlAgilityPack and XPath expressions.

    If run is an HtmlNode, then:

    1. run.SelectNodes("//div[@class='date']")
      Will will behave exactly like doc.DocumentNode.SelectNodes("//div[@class='date']")

    2. run.SelectNodes("./div[@class='date']")
      Will give you all the <div> nodes that are children of run node. It won't search deeper, only at the very next depth level.

    3. run.SelectNodes(".//div[@class='date']")
      Will return all the <div> nodes with that class attribute, but not only next to the run node, but also will search in depth (every possible descendant of it)

    You will have to choose between 2. or 3., depending on which one satisfy your needs :)

    0 讨论(0)
提交回复
热议问题