c# htmlagilitypack parse first link from div with class?

和自甴很熟 提交于 2019-12-12 01:12:29

问题


I am trying to parse the first link in the html code below /search?id=3

<div class="brs_col">
  <p>
    <a href="/search?id=3">
      <b>
        vastu shastra
      </b>
    </a>
  </p>
  <p>
    <a href="/search?id=1">
      <b>
        bygga
      </b>
      bastu
    </a>
  </p>
</div>

I've tried to select it with the following XPATH, but cant seem to get any of them to work:

//div[@class='brs_col']//p//a[@href]
//div[@class='brs_col']//p[0]//a[@href]
//div[@class='brs_col']//p//a[0][@href]

Any ideas?


回答1:


Try this:

var doc = new HtmlDocument();
doc.LoadHtml(@"<div class=""brs_col"">
                  <p><a href=""/search?id=3""><b>vastu shastra</b></a></p>
                  <p><a href=""/search?id=1""><b>bygga</b>bastu</a></p>
                </div>");
var hrefValue = doc.DocumentNode
                   .SelectSingleNode("//div[@class='brs_col']/p/a")
                   .Attributes["href"]
                   .Value;



回答2:


You can try this

doc.DocumentNode.SelectNodes("//a[@href]").FirstOrDefault();



回答3:


This if you sure that is the first url in the whole HTML document:

doc.DocumentNode.SelectSingleNode("//a").Attributes["href"].Value;

Or this if you sure that is the first ulr in the class brs_col

doc.DocumentNode.SelectSingleNode("//div[@class='brs_col']//a").Attributes["href"].Value;


来源:https://stackoverflow.com/questions/16662938/c-sharp-htmlagilitypack-parse-first-link-from-div-with-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!