Bonjour,
J’essaye 2 méthodes différentes pour le chargement de data web via excel :
La première fonctionne en pas à pas correctement
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 Sub LFWHOSC() 'Permet de cahrger les matchs sur internet et le numero d'identifiant du match "1080####" (col A) 'La procédure fonctionne en pas à pas mais pas en Run(F5) car il doit manqué une temporisation avant le Set IEDoc 'L'inconvénient de se chargement: Je ne peux pas toucher au PC, le timer plante 1 fois sur 4 'Application.ScreenUpdating = False 'Application.Visible = False Dim IE As New InternetExplorer Dim IEDoc As HTMLDocument Dim wsWSC As Worksheet Set wsWSC = ThisWorkbook.Worksheets("WSC") IE.navigate "https://www.whoscored.com/Regions/252/Tournaments/2/England-Premier-League" IE.Visible = False Set IEDoc = IE.document Dim htmlTabResultat As HTMLGenericElement Dim htmlLigneResultat As HTMLGenericElement Dim NumLigne As Byte Dim NumImg As Byte Set htmlTabResultat = IEDoc.body.all("tournament-fixture").Children(0) ' wsWSC.Range("A2") = "n° Match" wsWSC.Range("B2") = "Equipe n°1" wsWSC.Range("C2") = "Equipe n°2" wsWSC.Range("D2") = "Résultat" NumLigne = 3 For Each htmlLigneResultat In htmlTabResultat.Children If htmlLigneResultat.Children(0).innerText Like "*, * * ####" Then wsWSC.Cells(NumLigne, "A") = htmlLigneResultat.Children(0).innerText Else wsWSC.Cells(NumLigne, "A") = Mid(Left(htmlLigneResultat.outerhtml, InStr(htmlLigneResultat.outerhtml, "><") - 2), InStr(htmlLigneResultat.outerhtml, "data-id=") + 9) wsWSC.Cells(NumLigne, "B") = htmlLigneResultat.Children(1).innerText wsWSC.Cells(NumLigne, "C") = htmlLigneResultat.Children(2).innerText wsWSC.Cells(NumLigne, "D") = htmlLigneResultat.Children(3).innerText wsWSC.Cells(NumLigne, "E") = htmlLigneResultat.Children(4).innerText wsWSC.Cells(NumLigne, "F") = htmlLigneResultat.Children(5).innerText End If NumLigne = NumLigne + 1 Next 'Application.ScreenUpdating = True 'Application.Visible = True End Sub
La seconde méthode XMLHttp, je n’arrive pas à l’adapter en ayant un bout de code qui fonctionne.
Le bout de code qui fonctionne correctement :
Le code que j’essaye d’adapter sans succès:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 Public Sub parsehtml() Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement Dim i As Integer Set http = CreateObject("MSXML2.XMLHTTP") http.Open "GET", "https://news.ycombinator.com/", False http.send html.body.innerHTML = http.responseText Set topics = html.getElementsByClassName("athing") i = 2 For Each topic In topics Set titleElem = topic.getElementsByTagName("td")(2) Sheets(2).Cells(i, 1).Value = titleElem.getElementsByTagName("a")(0).innerText 'thevet Sheets(2).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).href '"https://..wiki Set detailsElem = topic.NextSibling.getElementsByTagName("td")(1) Sheets(2).Cells(i, 3).Value = detailsElem.getElementsByTagName("span")(0).innerText Sheets(2).Cells(i, 4).Value = detailsElem.getElementsByTagName("a")(0).innerText i = i + 1 Next End Sub
En mettant un espion sur topics, on s’apercoit qu’il n’y a pas d’items contrairement au bout de code fonctionnement.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 Public Sub parsehtml() Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, detailsElem As Object, topic As HTMLHtmlElement Dim i As Integer Set http = CreateObject("MSXML2.XMLHTTP") http.Open "GET", "https://www.whoscored.com/Regions/252/Tournaments/2/England-Premier-League", False http.send html.body.innerHTML = http.responseText 'Debug.Print http.responseText Set topics = html.getElementsByClassName("item alt") i = 2 For Each topic In topics i = i + 1 Next End Sub
avec l'espion identifiant les items
Sauriez-vous pourquoi ?
Mon but est d’obtenir la même chose qu’avec la première procédure utilisant IE.
Merci d’avance
Jérôme
Partager