2014-09-20 42 views
0

我已經開始做在Visual Basic中一些編程,我需要一些幫助。這可能是一個簡單的問題,但我無法弄清楚。獲取HTML元素「對於每個」循環

所以我創建了一個網頁瀏覽器,我想獲得在Facebook上的人的頭像,並在圖片框顯示出來,也該人的姓名和一個TextBox顯示。之後,程序應該將照片保存在硬盤上的文件夾中。

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted 

Dim elemCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img") 
    Dim fbPath As String = "C:\Users\" + Environment.UserName.ToString + "\Documents\FB Images\" 


    For Each curElement As HtmlElement In elemCollection 

     If curElement.OuterHtml.Contains("profilePic img") Then 
      PictureBox1.ImageLocation = curElement.GetAttribute("src") 'Showing the profile pic in picture box 
      TextBoxName.Text = curElement.GetAttribute("alt") 'Showing the name in a textbox 
      Dim NumberOfFiles As Integer = System.IO.Directory.GetFiles(fbPath).Length 
      Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height) 
      PictureBox1.DrawToBitmap(bmp, PictureBox1.ClientRectangle) 
      bmp.Save(System.IO.Path.Combine(fbPath, CStr(NumberOfFiles) + TextBoxName.Text + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg) 
     End If 

    Next 
End Sub 

所以程序應根據存在的文件夾路徑(例如「1喬治Johnson.jpg」)的文件數量與文件名保存圖片。但是會發生什麼,它會保存5個不同的圖像,因爲我猜想有多個HTML元素可以匹配這些屬性,因此For Each循環會帶來更多結果。

有沒有什麼辦法讓一個HTML元素,而無需使用這種循環,只是得到我想要的特定元素?

+0

使其評估增加更多的具體標準,以你的'If'檢查語句TRUE;只爲你想要的特定元素 – har07 2014-09-20 05:22:25

回答

0

如果你只是想在第一時間if語句的屬性爲true,加上「退出對於」結束時,如果前行。