Link Adresse auslesen Webbrowser

  • VB.NET

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von Vortex1991.

    Link Adresse auslesen Webbrowser

    Hi alle zusammen :)

    ich sitze derzeit an einem kleinem Projekt, von welchem ich noch nicht all zu viel erzählen möchte :P.
    Prinzipiell ist die Anwendung fertig, leider gefällt mir die aktuelle Umsetzung nicht, da sie den User dazu auffordert auf im Webbrowser angezeige Buttons manuell zu klicken.

    Also erst einmal bin ich mir auch überhaupt nicht sicher, ob es überhaupt möglich ist, link Adressen von Buttons auslesen zu lassen.

    Als kleine Anmerkung ich benutze folgende klasse, um den Webbrowser gegen den etwas besseren Edge Browser auszutauschen.

    VB.NET-Quellcode

    1. Namespace My
    2. Partial Friend Class MyApplication
    3. Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
    4. CreateBrowserKey()
    5. End Sub
    6. Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
    7. ' I don't like applications that defecate in the registry and then don't cleanup their own mess
    8. ' so remove the key
    9. RemoveBrowerKey()
    10. End Sub
    11. Private Const BrowserKeyPath As String = "\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION"
    12. Private Sub CreateBrowserKey(Optional ByVal IgnoreIDocDirective As Boolean = False)
    13. Dim basekey As String = Microsoft.Win32.Registry.CurrentUser.ToString
    14. Dim value As Int32
    15. Dim thisAppsName As String = My.Application.Info.AssemblyName & ".exe"
    16. ' Value reference: http://msdn.microsoft.com/en-us/library/ee330730(v=VS.85).aspx
    17. ' IDOC Reference: http://msdn.microsoft.com/en-us/library/ms535242(v=vs.85).aspx
    18. Select Case (New WebBrowser).Version.Major
    19. Case 8
    20. If IgnoreIDocDirective Then
    21. value = 8888
    22. Else
    23. value = 8000
    24. End If
    25. Case 9
    26. If IgnoreIDocDirective Then
    27. value = 9999
    28. Else
    29. value = 9000
    30. End If
    31. Case 10
    32. If IgnoreIDocDirective Then
    33. value = 10001
    34. Else
    35. value = 10000
    36. End If
    37. Case 11
    38. If IgnoreIDocDirective Then
    39. value = 11001
    40. Else
    41. value = 11000
    42. End If
    43. Case Else
    44. Exit Sub
    45. End Select
    46. Microsoft.Win32.Registry.SetValue(Microsoft.Win32.Registry.CurrentUser.ToString & BrowserKeyPath,
    47. Process.GetCurrentProcess.ProcessName & ".exe",
    48. value,
    49. Microsoft.Win32.RegistryValueKind.DWord)
    50. End Sub
    51. Private Sub RemoveBrowerKey()
    52. Dim key As Microsoft.Win32.RegistryKey
    53. key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(BrowserKeyPath.Substring(1), True)
    54. key.DeleteValue(Process.GetCurrentProcess.ProcessName & ".exe", False)
    55. End Sub
    56. End Class 'MyApplication
    57. End Namespace


    Was ich doch noch anmerken möchte ist, dass ich mit der Webbrowser.DocumentText Funktion nicht weiter komme, da im Quelltext keine Links dargestellt werden.

    Über Ansätze oder gute Ideen würde ich mich sehr freuen :)

    Liebe Grüße Vortex
    Was für Links auf welchen Buttons auf welcher Seite?

    Nachtrag: Buttons haben, in der Regel, im OnClick Event eine Funktion hinterlegt.
    Die deutsche Sprache ist Freeware, du kannst sie benutzen, ohne dafür zu bezahlen. Sie ist aber nicht Open Source, also darfst du sie nicht verändern, wie es dir gerade passt.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „MrTrebron“ ()

    Vortex1991 schrieb:

    link Adressen von Buttons

    Link !== Button

    Wenn wir von einer Website ausgehen, hätten wir "a"- & "button"-Tags.
    Ein Button hat nur die Standard-Events und dessen "Link" kann nur über das Event-Handling von Click, Hover, etc. angesteuert werden.

    Ein Link ("a"-Tag) hat innerhalb des Tags ein href-Attribut, was den targetLink, also die URL darstellt.

    Sprich rein aus dem Sourcecode einer Website bekommst du generell die URL von Links heraus, um die URL aus einem Button zu bekommen, müsstest du Zugriff auf die .js-Files etc. haben.
    (ein möglicher Workaround wäre, Links wie einen Button zu stylen..)
    Also was hast du in deiner Website?

    Auch wenn du nicht viel erzählen willst, wären auch Code-Snippets zur Website hilfreich.

    LG, Acr0most
    Wenn das Leben wirklich nur aus Nullen und Einsen besteht, dann laufen sicherlich genügen Nullen frei herum. :D
    Signature-Move 8o
    kein Problem mit privaten Konversationen zu Thema XY :thumbup:
    Habe es nun mittels einer C# library gelöst. Danke an die hilfreichen Antworten. Leider hat keine so geklappt wie es sollte, da die Seite erst einmal in ein neues Dokument läd.
    Für alle die interessiert sind, es ist ein youtube downloader.
    Die Adresse der API werde ich heraus Zensieren aus rechtlichen Gründen :)

    C#-Quellcode

    1. using HtmlAgilityPack;
    2. using OpenQA.Selenium.PhantomJS;
    3. using System;
    4. using System.Diagnostics;
    5. using System.Linq;
    6. using System.Threading;
    7. namespace VXYTDLL
    8. {
    9. public sealed class YoutubeVideo
    10. {
    11. private string url = "";
    12. public string Result { get; private set; }
    13. public bool IsFinished { get; private set; }
    14. public event EventHandler Finished;
    15. private void OnFinished(EventArgs e)
    16. {
    17. EventHandler handler = Finished;
    18. IsFinished = true;
    19. if (handler != null)
    20. handler(this, e);
    21. }
    22. public YoutubeVideo(string VideoID)
    23. {
    24. IsFinished = false;
    25. url = $"https://*************************/{VideoID}";
    26. var thread = new Thread(doStuff);
    27. thread.Start();
    28. }
    29. private void doStuff()
    30. {
    31. var doc = new HtmlDocument();
    32. HtmlNodeCollection nodes;
    33. try
    34. {
    35. var service = PhantomJSDriverService.CreateDefaultService();
    36. service.LocalToRemoteUrlAccess = true;
    37. service.HideCommandPromptWindow = true;
    38. PhantomJSDriver driver = new PhantomJSDriver(service);
    39. driver.Navigate().GoToUrl(url);
    40. //Ich bin C Sharp und whine bei jedem noch so kleinen fehlendem ; #NoAutoFill
    41. while(driver.PageSource.Contains("download-mp3-url btn audio q320") == false) {
    42. Thread.Sleep(1);};
    43. doc.LoadHtml(driver.PageSource);}
    44. catch
    45. {Console.WriteLine("ERROR: FAILED TO INITIALIZE WEBDRIVER");
    46. Result = null;
    47. OnFinished(new EventArgs());
    48. return;}
    49. nodes = doc.DocumentNode.SelectNodes("//*[contains(@class,'**************** audio q320')]");
    50. Result = "http:" + nodes[0].Attributes.AttributesWithName("href").ToArray()[0].Value;
    51. this.OnFinished(new EventArgs());
    52. }
    53. }
    54. }


    Und die entsprechende VB.net

    VB.NET-Quellcode

    1. Imports VXYTDLL, System.Net
    2. Public Class Form1
    3. Public S As String = "", wb As New WebBrowser With {.ScriptErrorsSuppressed = True}, DlFin As Boolean = False
    4. Public WithEvents WC As New WebClient
    5. Private Sub DL_Button_Click(sender As Object, e As EventArgs) Handles DL_Button.Click
    6. If Tracklist.Items.Count = 0 Then
    7. MsgBox("No Video was found. Please add Videos to the" & vbCrLf & "Tracklist by clicking the Button above.")
    8. Else
    9. If TextBox1.Text.Length = 0 Then
    10. MsgBox("Please select a Downloadpath.")
    11. Else
    12. Button1.Enabled = False
    13. download_start()
    14. End If 'L0L icH biN ein MMongO und Find die geschweiften klammern auf meiner tastatur nicht #VBProgrammierer.
    15. End If
    16. End Sub 'L0L icH biN ein MMongO und Find die geschweiften klammern auf meiner tastatur nicht #VBProgrammierer.
    17. Sub download_start()
    18. Dim STR As String = ""
    19. ALLDL.Value = 0
    20. CurrentDL.Value = 0
    21. ALLDL.Maximum = Tracklist.Items.Count
    22. For Each i As ListViewItem In Tracklist.Items
    23. Try
    24. WC.DownloadFileAsync(New Uri(i.SubItems(2).Text), TextBox1.Text & "\" & i.Text.Replace(":", "").Replace("?", "").Replace("*", "").Replace("<", "").Replace(">", "").Replace("|", "").Replace("/", "").Replace("\", "").Replace("""", "") & ".mp3")
    25. 'Application.DoEvents()
    26. DlFin = False
    27. Button1.Enabled = False
    28. While DlFin = False
    29. Application.DoEvents()
    30. End While
    31. Catch ex As Exception
    32. End Try
    33. Next
    34. End Sub
    35. Private Sub wc_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles WC.DownloadFileCompleted
    36. DlFin = True
    37. ALLDL.Value = ALLDL.Value + 1
    38. End Sub
    39. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    40. DL_Button.Enabled = False
    41. TextBox1.Text = My.Settings.Path
    42. End Sub
    43. Private Sub wc_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
    44. CurrentDL.Value = e.ProgressPercentage
    45. End Sub
    46. Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    47. For Each P As Process In System.Diagnostics.Process.GetProcessesByName("phantomjs")
    48. P.Kill()
    49. Next
    50. End Sub
    51. Private Sub TextBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseClick
    52. If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
    53. TextBox1.Text = FolderBrowserDialog1.SelectedPath
    54. End If
    55. My.Settings.Path = TextBox1.Text
    56. End Sub
    57. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    58. Dim Skipped As Integer = 0, Informer As String = ""
    59. Tracklist.Items.Clear()
    60. Button1.Text = "Getting Links - Please Wait..."
    61. infotext.Text = "Getting Tracks..."
    62. Button1.Enabled = False
    63. For Each link As String In Youtube_Links.Lines
    64. Try
    65. If link.Contains("https://www.youtube.com/watch?v=") = False And link.Contains("https://youtu.be/") = False And link.Contains("https://www.youtube.com/embed/") = False Then
    66. If link.Contains("") Then
    67. ' Playlist Events
    68. End If
    69. infotext.Text = "Skipping. Reason: No YouTube Link."
    70. Skipped = +1
    71. GoTo NoVideo
    72. End If
    73. ' ######################### Get VideoID ##############################
    74. Dim VideoID As String = ""
    75. If link.Contains("https://www.youtube.com/watch?v=") Then
    76. VideoID = link.Replace("https://www.youtube.com/watch?v=", "")
    77. ElseIf link.Contains("https://youtu.be/") Then
    78. VideoID = link.Replace("https://youtu.be/", "")
    79. ElseIf link.Contains("https://www.youtube.com/embed/") Then
    80. VideoID = link.Replace("https://www.youtube.com/embed/", "")
    81. End If
    82. ' ####################################################################
    83. ' ######################## Get Track Name ############################
    84. wb.Navigate(New Uri("https://www.youtube.com/embed/" & VideoID))
    85. While wb.ReadyState <> WebBrowserReadyState.Complete
    86. Application.DoEvents()
    87. End While
    88. ' ########################## Check Track #############################
    89. If wb.DocumentTitle = "YouTube" Or wb.DocumentTitle.Contains("YouTube") = False Then
    90. infotext.Text = "Skipping. Reason: Corrupt Video or Link."
    91. Skipped = +1
    92. GoTo NoVideo
    93. End If
    94. ' ####################################################################
    95. ' ########################### Get MP3 ################################
    96. Dim downloader As YoutubeVideo = New YoutubeVideo(VideoID)
    97. While downloader.IsFinished = False
    98. Application.DoEvents()
    99. End While
    100. Dim I As New ListViewItem With {.Text = wb.DocumentTitle.Replace(" - YouTube", "")}
    101. I.SubItems.Add(VideoID)
    102. I.SubItems.Add(downloader.Result)
    103. Tracklist.Items.Add(I)
    104. Catch ex As Exception
    105. MsgBox(ex.Message)
    106. End Try
    107. ' ######################################################################
    108. NoVideo:
    109. Next
    110. Button1.Text = "Add Links to Downloadlist"
    111. Select Case Tracklist.Items.Count
    112. Case 0
    113. Informer = "No Tracks found"
    114. Case 1
    115. Informer = Tracklist.Items.Count & " Track added to List"
    116. Case Else
    117. Informer = Tracklist.Items.Count & " Tracks added to List"
    118. End Select
    119. infotext.Text = Informer & " | " & Skipped & " skipped"
    120. Button1.Enabled = True
    121. If Tracklist.Items.Count = 0 Then
    122. DL_Button.Enabled = False
    123. Else
    124. DL_Button.Enabled = True
    125. End If
    126. End Sub
    127. End Class


    Hier das fertige Programm. Wenn solche Anwendungen nicht gestattet sind bitte um Rückmeldung :o

    Download V-Mp3