我需要帮助尝试将YouTube评论用户名从网络浏览器转移到列表框。我试图在没有YouTube API的情况下这样做。我一直在寻找一段时间而一无所获。
答案 0 :(得分:1)
我找到了一个解决方案,例如你有这个视频(http://www.youtube.com/watch?v=Sn67KEiDPLY)。现在你想得到评论作者。您应该创建一个webbrowser,在webbrowser导航到视频后,在documentcomplete上您可以检测到所有作者:
For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("span")
Dim ElementClass As String = "author"
If element.OuterHtml.Contains(ElementClass) Then
Dim aclass = "yt-uix-sessionlink yt-user-name"
If element.FirstChild.OuterHtml.Contains("yt-uix-sessionlink yt-user-name") Then
MsgBox(element.FirstChild.InnerText)
End If
End If
Next
MSgBox将向您展示每位作者。但是你可以将它们写入ListBox。您可以添加TextBox来管理视频的网址。所以最后的代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate(TextBox1.Text)
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
ListBox1.Items.Clear()
For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("span")
Dim ElementClass As String = "author"
If element.OuterHtml.Contains(ElementClass) Then
Dim aclass = "yt-uix-sessionlink yt-user-name"
If element.FirstChild.OuterHtml.Contains("yt-uix-sessionlink yt-user-name") Then
ListBox1.Refresh()
ListBox1.Items.Add(element.FirstChild.InnerText)
End If
End If
Next
End Sub
所以你可以添加这样的评论:
ListBox1.Items.Clear()
For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
Dim ElementClass As String = "comment-text"
If element.OuterHtml.Contains(ElementClass) Then
Dim divclass = "comment-text"
If element.Parent.InnerHtml.Contains(divclass) Then
ListBox1.Refresh()
ListBox1.Items.Add(element.InnerText)
End If
End If
Next
它适用于Webbrowser控件,但@ ObieMD5是正确的 - 使用Youtube API会更容易。