Question html link gathering and filtering

cruizrisner

New member
Joined
Jul 8, 2014
Messages
2
Programming Experience
Beginner
I am trying to obtain a specific link in the source of a webpage

here is the actions i have for the button that is to execute the event:

C#:
        private void button3_Click(object sender, EventArgs e)
        {
            HtmlElement download_link = webBrowser1.Document.GetElementById("dl_link");
            HtmlElementCollection links = download_link.GetElementsByTagName("a");
           
            
            string link = links[0].GetAttribute("href");
            if (link.StartsWith("http://www.sitenamehere.org/get?ab=128"))
            {
                webBrowser1.Navigate(link);
            }
            else
            {
                MessageBox.Show("Linking Error");
            }
            
            
            
        }

and here is the part of the webpage that its retrieving from:

HTML:
    <div id="dl_link" style="display: block;">
    <a style="display:none" href="/get?video_id=KMU0tzLwhbE&h=-1&r=-1.1">
    <b>Download</b>
    </a>
    <a style="display:none" href="/get?video_id=KMU0tzLwhbE&h=-1&r=-1.1">
    <b>Download</b>
    </a>
    <a style="display:none" href="/get?video_id=KMU0tzLwhbE&h=-1&r=-1.1">
    <b>Download</b>
    </a>
    <a href="/get?ab=128&video_id=KMU0tzLwhbE&h=54c0c03c1c950711ce66fa8e8e840d09&r=1404821250631.1522271859">
    <b>Download</b>
    </a>
    <a style="display:none" href="/get?video_id=KMU0tzLwhbE&h=-1&r=-1.1">
    <b>Download</b>
    </a>
    </div>

the issue is, it keeps giving me the false links because they appear before the correct one.

NOTE: KMU0tzLwhbE is file specific, so it changes depending on the file im going for. h=54c0c03c1c950711ce66fa8e8e840d09&r=1404821250631.1522271859 is session specific (refreshing page will change this). and h=-1&r=-1.1 (part of the fake links) is constant
 
try using

foreach (string link in Links)
{
 if (link == [COLOR=#3E3E3E]"http://www.sitenamehere.org/get?ab=128")
[/COLOR]{
do something with the link here
}
}


*Note* not tested

hope this helps
-InkedGFX
 
try using

foreach (string link in Links)
{
 if (link == [COLOR=#3E3E3E]"http://www.sitenamehere.org/get?ab=128")
[/COLOR]{
do something with the link here
}
}


*Note* not tested

hope this helps
-InkedGFX


thank you but i have already solved it, i used the follwing code

C#:
            HtmlElement download_link = webBrowser1.Document.GetElementById("dl_link");
            HtmlElementCollection links = download_link.GetElementsByTagName("a");

            foreach (HtmlElement element in links)
            {
                string style = element.GetAttribute("style");
                string link = element.GetAttribute("href");
                if (style != "display:none" && link.StartsWith("http://www.sitenamehere.org/get?ab=128"))
                {
                    webBrowser1.Navigate(link);
                    break;

                }
                
            }
 
Back
Top Bottom