Oppure

Loading
27/06/14 1:02
Light
Salve, ho un problema.
Lavorando con il WebBrowser sono riuscito a prelevare un url di un'immagine con questo comando:

Dim theElementCollection As HtmlElementCollection

theElementCollection = Webbrowser1.Document.GetElementsByTagName("img";)


For Each curElement As HtmlElement In theElementCollection

If curElement.GetAttribute("src";).ToString().Contains("660187210687117";) Then


PictureBox1.ImageLocation = curElement.GetAttribute("src";).ToString()


MsgBox(curElement.GetAttribute("src";).ToString())
End If
Next

Codice Html della pagina:

<a class="coverWrap coverImage" href="facebook.com/…; rel="theater" ajaxify="facebook.com/…; title="Immagine di copertina" id="fbCoverImageContainer" data-cropped="1"><img class="coverPhotoImg photo img" src="scontent-b-mxp.xx.fbcdn.net/hphotos-xpf1/t1.0-9/…; style="top:-231px;width:100%" data-fbid="660187210687117" alt="Immagine di copertina"><div class="coverBorder"></div><img class="coverChangeThrobber img" src="fbstatic-a.akamaihd.net/rsrc.php/v2/yk/r/…; alt="" width="16" height="16"></a>

Praticamente ho estratto "src" tramite: "data-fbid="660187210687117"
È possibile estrarre direttamente l'url dell'immagine ovvero:

scontent-b-mxp.xx.fbcdn.net/hphotos-xpf1/t1.0-9/…;

Tramite il nome della classe? Oppure è possibile estrarre il valore data-fbid?
Si può fare? Potete aiutarmi? Grazie in anticipo.

In rete ho trovato questo:

For Each ele As HtmlElement In Webbrowser1.Document.All

If ele.GetAttribute("src";).ToLower.Contains(".jpg";) Then
Dim imgsrc As String = ele.GetAttribute("src";)
MsgBox(imgsrc)
End If

Next

Però questo estrae tutte le immagini .jpg
aaa
27/06/14 8:57
GN
Non sono sicuro di aver capito esattamente cosa vuoi fare, comunque a quanto ho capito si tratta di estrarre l'immagine di copertina da una pagina/profilo facebook. In tal caso ti basta modificare il codice che hai postato in modo che controlli l'attributo class e non src:
            If curElement.GetAttribute("class") = "coverPhotoImg photo img" Then 
                PictureBox1.ImageLocation = curElement.GetAttribute("src").ToString() 
                MsgBox(curElement.GetAttribute("src").ToString()) 
            End If 
aaa
27/06/14 15:45
Light
Così?

Dim theElementCollection As HtmlElementCollection

theElementCollection = Webbrowser1.Document.GetElementsByTagName("img";)


For Each curElement As HtmlElement In theElementCollection

If curElement.GetAttribute("class";) = "coverPhotoImg photo img" Then
PictureBox1.ImageLocation = curElement.GetAttribute("src";).ToString()
MsgBox(curElement.GetAttribute("src";).ToString())
End If
Next
Non funziona, dove sbaglio?
Ultima modifica effettuata da Light 27/06/14 15:56
aaa
27/06/14 18:05
GN
Strano, a me sembra giusto. Controlla che il valore dell'attributo class sia effettivamente quello (se hai copiaincollato il codice che ti ho postato potrebbe non essere esatto dato che l'ho scritto di fretta); se non dovessi risolvere, prova a fare un po' di debug per scoprire perchè non lo trova
Ultima modifica effettuata da GN 27/06/14 18:17
aaa
27/06/14 18:25
Light
Niente non funziona
aaa
28/06/14 11:07
Uno sciame di API
@GN Il codice sembra giusto anche a me, credo si tratti più di un bug del controllo WebBrowser che un errore nel codice :)
Per quanto riguarda il problema, visto che GetAttribute("class";) non ritorna alcun valore, si può risolvere con il codice:
Dim images As System.Windows.Forms.HtmlElementCollection
images = WebBrowser1.Document.GetElementsByTagName("img")
For Each Img As System.Windows.Forms.HtmlElement In images
	If Img.OuterHtml.Contains("class=""coverPhotoImg photo img""") Then
		PictureBox1.ImageLocation = Img.GetAttribute("src")
		Exit For
	End If
Next

inserito nell'evento DocumentCompleted del controllo WebBrowser. Non credo ci sia bisogno di alcuna spiegazione sul codice in quanto è molto semplice :k:
Spero di essere stato di aiuto :)
aaa
28/06/14 15:01
Light
Grazie,funziona ;)
aaa
28/06/14 18:48
GN
Hai ragione, cercando un po' ho appena scoperto che usare getAttribute per la classe crea problemi anche ad altri: social.msdn.microsoft.com/Forums/vstudio/en-US/78baa8fe-72bc-486c-9de2-0a17ef3b66de/…
aaa