HTML XPath Searching by class name

asp.net c# html html-agility-pack xpath

Question

In C#, I'm having an issue with xpath.
I want to locate every component of this construction.
I have 10 links, and they are all structured as follows:

<div class="PartialSearchResults-item" data-zen="true">
<div class="PartialSearchResults-item-title">
<a class="PartialSearchResults-item-title-link result-link"target="_blank" href='https://www.google.com/'> Google</a>
    </div>
<p class="PartialSearchResults-item-url">www.google.com</p>
<p class="PartialSearchResults-item-abstract">Search the world.</p>
   </div>

for instance, I want to obtain "Google," "www.google.com," and "Search the globe" from this sample.

var titles = hd.DocumentNode.SelectNodes("//div[contains(@class, 'PartialSearchResults-item')]");
string link;
foreach (HtmlNode node in titles){
string description = node.SelectSingleNode(".//*[contains(@class,'PartialSearchResults-item-abstract')]").InnerText;

link = node.SelectSingleNode(".//*[contains(@class,'PartialSearchResults-item-url')]").InnerText;

 string title = node.SelectSingleNode(".//a[contains(@class,'PartialSearchResults-item-title-link result-link')]").InnerText;}

But I get a null reference error

1
3
5/8/2017 4:34:48 AM

Accepted Answer

The query where you are obtaining the results is where the issue lies.titles . You're trying to find a div that'sclass characteristic includesPartialSearchResults-item this is the root node of your item. However, there are additional nodes that are relevant to your search, such as the div with classPartialSearchResults-item-title also answering your question After choosing these two divs, you iterate over them in an attempt to determine their total of child nodes. The first time, your code will run successfully since you get the correct node, but the second time, you have the node with class.PartialSearchResults-item-title which have only onea so you will receiveNullReferenceException Because you're attempting to get the value of the description in the second iteration while you're inquiring for it,InnerText possession ofnull object

string description = node.SelectSingleNode(".//*[contains(@class,'PartialSearchResults-item-abstract')]").InnerText;

I advise against usingcontains Your root node contains only one class in your situation.PartialSearchResults-item so you may ask it questions like these.

var titles = hd.DocumentNode.SelectNodes("//div[@class='PartialSearchResults-item']");
7
5/7/2017 5:27:26 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow