Substring (string.IndexOf) is returning unwanted parts


I'm capturing a music site . I would like to return only 2 information artist and music. It is in this code snippet:

<div class="nowOnAir">
            <a href="" onclick="javascript:loadUrl(this.href);return false;" class="autore" title="Scopri tutto su edoardo bennato">
                edoardo bennato            </a><br />
            <span>le ragazze fanno grandi sogni</span>


Artist = edoardo bennato

music = le ragazze fanno grandi sogni

I'm trying to recover like this:

string musica = resposta.Substring(resposta.IndexOf("<span>"), resposta.IndexOf("</span>"));
string artista = resposta.Substring(resposta.IndexOf("autore"), resposta.IndexOf("</a><br />"));

In the case of artist ok, I know there are more items, but the music for me would be 100% correct, but it returns the following content in the song:

"<span>le ragazze fanno grandi sogni</span>\n            \n        </div>\n     \t\n        \n        \n                \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        <div class=\"iTunes\">\n        \n                \n           <a href=\"\" target=\"_blank\">\n           <img src=\"\" title=\"Acquista su Amazon\"  alt=\"Acquista su Amazon\" />\n           </a>\n        \n        \n        \n\t\t       <!-- -->\n        \t<a style=\"background:none;\" href=\"\" target=\"_blank\"><img src=\"\" title=\"Scarica su itunes\"  alt=\"scarica\"/></a>\n       \n           \t\t</div>\n\t\t\n\t\t<script>\n        $(document).ready(function(){\n            var mostra=0;\n            $(\".last5\").mousedown(function(){\n                if(mostra==0){\n                    $(\".songs\").fadeIn(\"fast\");\t\n                    mostra=1;\t\n                }else{\n                    $(\".songs\").fadeOut(\"fast\");\t\n                    mostra=0;\t\n                }\n            });\n        \n        \n        });\n\t\t\n        </script>\n       \n        \n        \n        \n     \n    <div class=\"fotoArtista\">    \n       \n    \t<a href=\"\" onclick=\"javascript:loadUrl(this.href);return false;\" title=\"Guarda tutte le foto di claudio baglioni\">Foto: 53</a>\n   \n        \n    \t<a href=\"\"  onclick=\"javascript:loadUrl(this.href);return false;\" title=\"Guarda tutte i video di claudio baglioni\">Video: 35</a>\n\t\n    \t\n    </div>\n        <div class=\"newsArtista\">\n    \t<a href=\"\"  onclick=\"javascript:loadUrl(this.href);return false;\">\n    \t    Tutte le news\n        </a>\n\t</div>\n        \n        \n        \n        \n        \n        <div class=\"correlati\">\n            <h3>Artisti consigliati</h3>\n            <ul>\n                                <li><a href=\"\"  onclick=\"javascript:loadUrl(this.href);return false;\" title=\"Emma\"><img src=\"\" border=\"0\" ></a></li>\n                                <li><a href=\"\"  onclick=\"javascript:loadUrl(this.href);return false;\" title=\"Marco Mengoni\"><img src=\"http://sta"

What's wrong?

asked by anonymous 03.08.2015 / 03:51

1 answer


You're really getting the wrong positions.

The beginning is not considering the characters of what you are looking for. So if you're looking for <span> you have to get 6 characters ahead so you do not get your own search string.

The second parameter expects how many characters you want to pick, not the position. Then you should find the string that does the final wedding and should subtract what has already been disregarded before in the case the value of the first parameter. This way you have the number of characters and not the position.


using static System.Console;

public class Program {
    public static void Main() {
        var resposta = @"<div class=""nowOnAir"">
            <a href="""" onclick=""javascript:loadUrl(this.href);return false;"" class=""autore"" title=""Scopri tutto su edoardo bennato"">
                edoardo bennato            </a><br />
            <span>le ragazze fanno grandi sogni</span>

        var inicio = resposta.IndexOf("<span>") + 6;
        var musica = resposta.Substring(inicio, resposta.IndexOf("</span>") - inicio);
        inicio = resposta.IndexOf("autore") + 6;
        var artista = resposta.Substring(inicio, resposta.IndexOf("</a><br />") - inicio);

See working on dotNetFiddle .

Note that the artist's result is wrong as you recognize it. Adapt to what you need now. You already know where you were wrong.

One detail: getting parsing pages from third parties is asking to have problems, unless the creator of the page claims that they will never make changes to it. Just do it in desperation.

03.08.2015 / 04:07