如何从VBA上的网络表访问数据?

时间:2018-12-20 15:07:48

标签: html vba excel-vba excel-2007

我正在尝试检索网站上的某些信息,为此我必须先进行连接。 但是之后,我必须检索表中的数据以将其放在Excel上。

网站上的HTML:

<table class="tab_script"></table>
<table class="tab_script">
   <tbody>
      <tr>
         <td>N° commande SFR</td>
         <td>Nom</td>
         <td>Prenom</td>
         <td>N° Mobile</td>
         <td>N° Fixe</td>
         <td>Ville</td>
         <td>Code Postal</td>
         <td>Num. Intervention</td>
         <td>date rdv</td>
     </tr>





    <tr bgcolor="#E5E5E5">
       <form method="POST"></form>
          <td></td>
          <td>name</td>
          <td></td>
          <td>telf</td>
          <td>telp</td>
          <td>PLOUGUERNEAU</td>
          <td>29880</td>
          <td>
             <a href="index.php?id=13&amp;statut=2&amp;id_inter=183530401">183530401</a></td>
          <td style="background-color:red">20/12/2018 10:00</td>
     </tr>


     <tr bgcolor="#FFFFFF">
        <form method="POST"></form>
        <td></td>
        <td>name</td>
        <td></td>
        <td>numf</td>
        <td>nump</td>
        <td>PLOURIN LES MORLAIX</td>
        <td>29600</td>
        <td>
           <a href="index.php?id=13&amp;statut=2&amp;id_inter=183531538">183531538</a> 
        </td>
        <td style="background-color:#FF7F00">20/12/2018 14:30</td>
     </tr>
  //.
  //.
  //.

为此,我在VBA中已尽力而为,但这没用。

VBA中的代码(经过一些测试):

Set ie = CreateObject("InternetExplorer.Application") ' Création d'objet Internet Explorer
ie.Visible = True
ie.Navigate ("https://connect-partner.sfr.fr/") ' Redirection vers cette page

' attente de fin de chargement
Do Until ie.readyState = 4
    DoEvents
Loop


'login
 Set DOCelement = ie.document.getElementsByName("login").Item
 On Error Resume Next
 DOCelement.Value = "IPCA2935" ' Affecter l'identifiant à l'élément "login"


'password
 Set DOCelement = ie.document.getElementsByName("password").Item
 DOCelement.Value = "pfTTD8IH" ' Affecter le mot de passe à l'élément "password"

'connexion
ie.document.all("connexion").Click ' Click sur le bouton Connexion
'
' Redirection vers Intervention -> Plannifiées
'
'Set maPageHtml = webBrowser1.Document
'maPageHtml.parentWindow.execScript "'disp_menu(4)'", "Javascript"

ie.document.all.Item
Call ie.document.parentWindow.execScript("disp_menu(4)", "JavaScript") 'DOESNT WORK
Application.Wait (2000)
'HTMLDocparentWindow.execScript "window.confirm = disp_menu(4) {return true;};"

ie.document.getElementsByTagName("a")(4).Click

'
'TESTS
'

'Dim td As MSHTML.IHTMLElementCollection
'Dim tr As MSHTML.IHTMLElementCollection
'Dim a As MSHTML.IHTMLElementCollection
'Dim trObj As MSHTML.HTMLGenericElement
'Dim tdObj As MSHTML.HTMLGenericElement
'Dim aObj As MSHTML.HTMLGenericElement

'Set tr = HTMLDoc.getElementsByTagName("tr")
'For Each trObj In tr
'    Set td = trObj.getElementsByTagName("td")
'    For Each tdObj In td
'        ' Faire quelque chose pour chaque objet td
'        'Set ws1 = Sheets("Planifiees").Range("A1")
'        Set nom = getElementsByTagName("td")(1).innerText.Sheets("Planifiees").Range("A1")
'        Set adresse = tdObj.innerText.Sheets("Planifiees").Range("B1")
'        Debug.Print (nom)
'        For Each aObj In a
'            Set lien = getElementsByTagName("a").Click
'
'        Next
'    Next
'Next

'
' TESTS
'

Dim html As New HTMLDocument
Dim tableau As HTMLTable
Dim c As Long, r As Long
Dim tr As Object, tc As Object

Set tableau = ie.document.getElementsByClassName("tab_script")

With ThisWorkbook.Worksheets("Planifiees")
    For Each tr In tableau.getElementsByTagName("tr")
        r = r + 1: c = 1
        For Each tc In tr.getElementsByTagName("td")
            .Cells(r, c).Value = tc.innerText
            c = c + 1
        Next
    Next
End With

之后,我想访问超链接,但这是另一个故事。 :) 如果有人可以帮助我,那就太好了!

PS:对不起,我的英语,我是法国人

0 个答案:

没有答案