IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Web & réseau Delphi Discussion :

récupérer des infos d'une page web


Sujet :

Web & réseau Delphi

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Juin 2004
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2004
    Messages : 35
    Points : 24
    Points
    24
    Par défaut récupérer des infos d'une page web
    Bonjour, je souhaite récupérer les infos en rouge

    <div title="Cliquez pour les détails du match!" class="event__match event__match--scheduled event__match--last event__match--inline event__match--oneLine" id="g_1_CK3ne74R">
    <div class="event__check"></div>
    <div class="event__time">21:00</div>
    <div class="event__participant event__participant--home">Tottenham</div>
    <div class="event__scores"><span class="icon icon--preview"></span></div>
    <div class="event__participant event__participant--away">Ajax</div>
    <div class="event__icon icon--tv"></div>
    <span class="event__live--icon icon icon--live " data-bookmaker-id="141"></span></div>

    J 'arrive a recuperer l ID "g_1_CK3ne74R" dans la page web ... mais le reste...

    Merci.

  2. #2
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 030
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 030
    Points : 40 928
    Points
    40 928
    Billets dans le blog
    62
    Par défaut
    Bonjour,

    il y a tellement de manière de faire en fonction de la version Delphi que vous utilisez qu'il faudrait peut-être commencer par nous fournir ce renseignement.
    Ensuite vous nous indiquerez comment (code) vous obtenez cette info et là on pourra certainement répondre
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Juin 2004
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2004
    Messages : 35
    Points : 24
    Points
    24
    Par défaut
    J utilise delphi 6.

    voici mon code :
    Je cherche toutes les class "event__match...." et je récupère les ID (affichage dans la grille1).
    Et je voudrais récupérer toutes les infos interne de chaque ID (affichage dans la grille2).

    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
    45
    46
    47
    48
    49
    50
    procedure TForm1.Button3Click(Sender: TObject);
    var
      Document, Doc: IHTMLDocument2;     
      Body: IHTMLElement2;          
      Tags, Tags2, Coll: IHTMLElementCollection; 
      Anchor : IHTMLAnchorElement;
      Tag, Elemt, event : IHTMLElement;            
      I, Inx, Inx2: Integer;                
      S : String;
    begin
      // Check for valid document: require IHTMLDocument2 interface to it
      if not Supports(WebBrowser1.Document, IHTMLDocument2, Document) then
        raise Exception.Create('Invalid HTML document');
      // Check for valid body element: require IHTMLElement2 interface to it
      if not Supports(Document.body, IHTMLElement2, Body) then
        raise Exception.Create('Can''t find <body> element');
     
      Tags := Body.getElementsByTagName('*');
     
     
      inx  := 0;
      inx2 := 0;
      StringGrid1.RowCount := 1;
      StringGrid2.RowCount := 1;
      for I := 0 to Pred(Tags.length) do
      begin
     
        Tag := Tags.item(I, EmptyParam) as IHTMLElement;
        if  (Tag.className = 'event__match event__match--live event__match--last highlighted event__match--inline event__match--oneLine') or
            (Tag.className = 'event__match event__match--scheduled event__match--last event__match--inline event__match--oneLine') or
            (Tag.className = 'event__match event__match--last event__match--inline event__match--oneLine') then
        begin
          Inc(Inx);
          StringGrid1.RowCount := Inx+1;
          StringGrid1.Cells[0,Inx] := IntToStr(Inx);
          StringGrid1.Cells[1,Inx] := Tag.id;
          StringGrid1.Cells[2,Inx] := Tag.innerText;
          StringGrid1.Cells[3,Inx] := Tag.className;
     
     
          Elemt := (WebBrowser1.Document as IHTMLDocument3).getElementById(Tag.id);
            Inc(Inx2);
            StringGrid2.RowCount := Inx2+1;
            StringGrid2.Cells[0,Inx2] := IntToStr(Inx2);
            StringGrid2.Cells[1,Inx2] := WebBrowser1.OleObject.Document.GetElementByID(Tag.id).Id;
            StringGrid2.Cells[2,Inx2] := WebBrowser1.OleObject.Document.GetElementByID(Tag.id).getElementsByTagName('event_time');
     
        end;
      end;
    end;


    Le code de la page web :

    Code Html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    <div title="Cliquez pour les détails du match!" class="event__match event__match--scheduled event__match--last event__match--inline event__match--oneLine" id="g_1_CK3ne74R">
    <div class="event__check"></div>
    <div class="event__time">21:00</div>
    <div class="event__participant event__participant--home">Tottenham</div>
    <div class="event__scores"><span class="icon icon--preview"></span></div>
    <div class="event__participant event__participant--away">Ajax</div>
    <div class="event__icon icon--tv"></div>
    <span class="event__live--icon icon icon--live " data-bookmaker-id="141"></span></div>

  4. #4
    Expert éminent sénior
    Avatar de Paul TOTH
    Homme Profil pro
    Freelance
    Inscrit en
    Novembre 2002
    Messages
    8 964
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2002
    Messages : 8 964
    Points : 28 430
    Points
    28 430
    Par défaut
    personnellement je ferais plutôt une fonction d'extraction sous Delphi à partir du source

    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
     
    // au erreur de syntax et index près...
    function extract(const HTML, class: string): string;
    var
      Start: Integer;
      Stop : Integer;
    begin
      Start := Pos('<div class="' + class + '">', HTML);
      if Start= 0 then
        Exit('');
      Inc(Start, 14 + Length(class));  
      Stop := Pos('</div>', HTML, Start);
      if Stop <= Start then
       Result := ''
      else
      Result := Copy(HTML, Start, Stop - Start); 
    end;
     
    begin
      time := extract(HTML, 'event__time');
      home := extract(HTML, 'event__participant event__participant--home');
      away := extract(HTML, 'event__participant event__participant--away');
    end;
    Developpez.com: Mes articles, forum FlashPascal
    Entreprise: Execute SARL
    Le Store Excute Store

  5. #5
    Rédacteur/Modérateur

    Avatar de SergioMaster
    Homme Profil pro
    Développeur informatique retraité
    Inscrit en
    Janvier 2007
    Messages
    15 030
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 67
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Janvier 2007
    Messages : 15 030
    Points : 40 928
    Points
    40 928
    Billets dans le blog
    62
    Par défaut
    Delphi 6 ! voilà pourquoi j'avais insisté pour obtenir la version car ma première idée était de dire d'utiliser les expressions régulières, avec D6 c'est plutôt raté
    Mon second choix aurait été sur un xmldoc mais encore une fois, je pense que c'est au-dessus de D6 (voire bien au-dessus)
    La proposition de Paul est donc très certainement la plus efficace
    MVP Embarcadero
    Delphi installés : D3,D7,D2010,XE4,XE7,D10 (Rio, Sidney), D11 (Alexandria), D12 (Athènes)
    SGBD : Firebird 2.5, 3, SQLite
    générateurs États : FastReport, Rave, QuickReport
    OS : Window Vista, Windows 10, Windows 11, Ubuntu, Androïd

  6. #6
    Membre à l'essai
    Profil pro
    Inscrit en
    Juin 2004
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2004
    Messages : 35
    Points : 24
    Points
    24
    Par défaut
    Je ne suis pas trop caler en delphi pour le WEB...
    Sinon j'ai aussi delphi EX8...

    J'ai aussi essayé :
    StringGrid2.Cells[2,Inx2] := WebBrowser1.OleObject.Document.GetElementByID(Tag.id).getElementsByTagName('event_time');

    Mais ça me revoit :
    [object HTMLCollection]

  7. #7
    Expert éminent sénior
    Avatar de Paul TOTH
    Homme Profil pro
    Freelance
    Inscrit en
    Novembre 2002
    Messages
    8 964
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2002
    Messages : 8 964
    Points : 28 430
    Points
    28 430
    Par défaut
    Citation Envoyé par tomkiki Voir le message
    Je ne suis pas trop caler en delphi pour le WEB...
    Sinon j'ai aussi delphi EX8...

    J'ai aussi essayé :
    StringGrid2.Cells[2,Inx2] := WebBrowser1.OleObject.Document.GetElementByID(Tag.id).getElementsByTagName('event_time');

    Mais ça me revoit :
    [object HTMLCollection]
    normal, getElementsByTagName() renvoie la liste des tous les enfants dont le TAG est "event_time"...et là c'est une liste vide puisque ce sont tous des DIV

    à la rigueur, pour récupérer le contenu de deuxième enfant
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
      time := WebBrowser1.OleObject.Document.GetElementByID(Tag.id).children[1].innerHTML;
    Developpez.com: Mes articles, forum FlashPascal
    Entreprise: Execute SARL
    Le Store Excute Store

  8. #8
    Membre éclairé
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    707
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 707
    Points : 777
    Points
    777
    Par défaut
    Perso j'utilise un composant appelé DIHtmlParser... que j'ai acheté il y a de nombreuses années.

    Mais il existe un certain nombre d'outils plus ou moins élaborés disponibles gratuitement, il suffit de faire une recherche genre "delphi dom parser" ou "delphi html parser".

  9. #9
    Membre chevronné Avatar de philnext
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    1 552
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 1 552
    Points : 1 780
    Points
    1 780
    Par défaut Parseur
    Je pense que, comme indiqué, l'utilisation d'un parseur est la meilleure solution dans le style : https://github.com/sandbil/HTML-Parser

Discussions similaires

  1. Récupérer des infos d'une page Web avec identifiant et mdp en VBA
    Par sebfch dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 30/01/2017, 07h48
  2. [RegEx] Récupérer des infos d'une page web
    Par Chgro dans le forum Langage
    Réponses: 9
    Dernier message: 20/03/2007, 07h44
  3. récupérer des infos d'une page web (TWebBrowser)
    Par LotfiB dans le forum C++Builder
    Réponses: 3
    Dernier message: 16/01/2007, 21h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo