XPath expression not working in HtmlAgilityPack

I know this may be out of my XPath noob, but let me ask to be sure, because I have searched Google enough.

I have a website and want to receive news headlines: www.farsnews.com (this is Persian)

Using the FireBug and FireXpath extensions under firefox and manually, I retrieve and test several Xpath expressions that match the headers, for example:

* html/body/div[2]/div[2]/div[2]/div[*]/div[2]/a/div[2]
* .//*[@class="topnewsinfotitle "]
* .//div[@class="topnewsinfotitle "]

I also tested them with the XPather extension and they seem to work very well, but when I test them ... SelectNodes returns null!

Any clue or clue?

here is the code snippet:

listBox2.ResetText();

HtmlAgilityPack.HtmlWeb w = new HtmlAgilityPack.HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = w.Load("http://www.farsnews.com");
HtmlAgilityPack.HtmlNodeCollection nc = doc.DocumentNode.SelectNodes(".//div[@class=\"topnewsinfotitle \"]");

listBox2.Items.Add(nc.Count+" Items selected!");

foreach (HtmlAgilityPack.HtmlNode node in nc) {
            listBox2.Items.Add(node.InnerText);
        }

Thank.

+3
source share
1 answer

. , Dialecticus, , .

//div[@class='topnewsinfotitle ']/text()

' ', . : http://xmltools.dk/EQA-ACA6

//div[@class='topnewsinfotitle']/text()

, .: http://xmltools.dk/EgA2APAj

, , (http://xmltools.dk/EwA8AJAW):

//div[contains(@class, 'topnewsinfotitle')]/text()

( , , , XPath, /text(), , )

, , XML (, RSS ATOM) JSON, , , .

+4

Source: https://habr.com/ru/post/1762033/


All Articles