1

I try parse image url from html with HtmlAgilityPack. In html doc I have img tag :

<a class="css_foto" href="" title="Fotka: MyKe015">
   <span>
      <img src="http://213.215.107.125/fotky/1358/93/v_13589304.jpg?v=6" 
           width="176" height="216" alt="Fotka: MyKe015" />
   </span>
</a>

I need get from this img tag atribute src. I need this: http://213.215.107.125/fotky/1358/93/v_13589304.jpg?v=6.

I know this:

  1. Src atribute consist url, url start with http://213.215.107.125/fotky
  2. I know value of alt atribute Url have variable lenght and also html doc consist other img tags with url, which start with http://213.215.107.125/fotky
  3. I know alt attribute of img tag (Fotka: Myke015))

Any advance, I try many ways, but nothing works good.

Last I try this:

    List<string> src;

    var req = (HttpWebRequest)WebRequest.Create("http://pokec.azet.sk/myke015");
    req.Method = "GET";

    using (WebResponse odpoved = req.GetResponse())
    {
        var htmlDoc = new HtmlAgilityPack.HtmlDocument();
        htmlDoc.Load(odpoved.GetResponseStream());

        var nodes = htmlDoc.DocumentNode.SelectNodes("//img[@src]");
        src = new List<string>(nodes.Count);

        if (nodes != null)
        {
            foreach (var node in nodes)
            {
                if (node.Id != null)
                    src.Add(node.Id);
            }
        }
    }

2 Answers 2

3

Your XPath selects the img nodes, not the src attributes belonging to them.

Instead of (selecting all image tags that have a src attribute):

var nodes = htmlDoc.DocumentNode.SelectNodes("//img[@src]");

Use this (select the src attributes that are child nodes of all img elements):

var nodes = htmlDoc.DocumentNode.SelectNodes("//img/@src");
Sign up to request clarification or add additional context in comments.

4 Comments

Oded, how can I specify in Xpath value of src atribute. src="Fotka: miky015"
@Lucas - at this point, you can use node.InnerText to get the value.
Oded I use your advace, but I get error: '//img@src' has an invalid token, if I used your Xpath exp "//img@src".
@Lucas - apologies, I missed a /. It should be //img/@src.
0

This XPath 1.0 expression:

//a[@alt='Fotka: MyKe015']/@src

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.