Html Agility Pack, site search for a specific line of words

I use the Html Agility Pack for this task, basically I have a URL, and my program should read the contents of the html page on it, and if it finds a line of text (for example: John had three apples "), he should change shortcut text to "Found."

I tried to do this with contains, but I think it only checks one word.

var nodeBFT = doc.DocumentNode.SelectNodes("//*[contains(text(), 'John had three apples')]");

if (nodeBFT != null && nodeBFT.Count != 0)
    myLabel.Text = "Found it";

EDIT: The rest of my code, now with an ako attempt:

if (CheckIfValidUrl(v)) // foreach var v in a list..., checks if the URL works
{
    HtmlWeb hw = new HtmlWeb();
    HtmlDocument doc = hw.Load(v);

    try
    {
        if (doc.DocumentNode.InnerHtml.ToString().Contains("string of words"))
        {
            mylabel.Text = v;
        }
    ...
+4
source share
2 answers

- . text(). text() contains() , , , :

doc.DocumentNode.SelectNodes("//*[contains(., 'John had three apples')]");

contains(., '...') , . , -, XPath , , :

<span>John had <br/>three <strong>apples</strong></span>

, XPath , node, , :

doc.DocumentNode.SelectNodes("//*[text()[contains(., 'John had three apples')]]");

, HTML, , , , .

+6

:

if (doc.DocumentNode.InnerHtml.ToString().Contains("John had three apples"))
    myLabel.Text="Found it";
+1

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


All Articles