[résolu] Lire le code HTML d'une page IE

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Christophe
Messages : 43
Inscription : lun. 22/oct./2018 19:34
Localisation : Haisnes (62)

[résolu] Lire le code HTML d'une page IE

Message par Christophe »

Bonjour, existe t-il un moyen pour pouvoir récuperer le code HTML d'une page Web d'Internet Explorer avec PureBasic
Avec le code ci joint livré avec COMate j'y arrive mais uniquement pour un WebGadget. J'ai parcouru tous les forums français et étranger mais je n'ai malheureusement pas trouvé.
Le but étant de récuperer quelques infos sur la page pour créer une liste, ce qui me fera gagner un temps considérable (pour mon boulot)
Toute aide est la bienvenue. Merci

Code : Tout sélectionner

;***COMate***  COM automation through iDispatch.
;*===========
;*
;*Web gadget demo. (Advanced users!)  Based upon code by hm.
;*
;*This demo not only shows how to obtain a COMateObject from a web-gadget, but also sets a 'global' event handler.
;*
;*This demo also shows how to over-ride the COMateObject SetEventHandler() method and direct COMate to seek out a specific
;*'out-going interface'. Left to it's own devices, COMatePLUS will utilise the first suitable 'connection point' and out-going interface
;*that it encounters. In the case of a web gadget, we direct COMatePLUS to directly seek out a connection point for the newer DWebBrowserEvents2
;*interface.

;*I did state that this demo is for advanced users!
;/////////////////////////////////////////////////////////////////////////////////


IncludePath "..\"
XIncludeFile "COMatePLUS.pbi"

Define.COMateObject browser, document


Enumeration 
  #Web_Gadget 
  #Button_Start 
EndEnumeration 


;/////////////////////////////////////////////////////////////////////////////////
;The following is our event callback for our web-gadget object.
;The 'Object' parameter will contain the COMate object upon which was used the \SetEventHandler() method; in our case it will point
;to our browser object.
Procedure EventProc(Object.COMateObject, eventName$, parameterCount) 
  Debug eventName$ 
EndProcedure 
;/////////////////////////////////////////////////////////////////////////////////


If OpenWindow(0, 302, 15, 600, 300, "WebGadget Events Test",  #PB_Window_SystemMenu | #PB_Window_TitleBar ) 
  WebGadget(#Web_Gadget, 20, 20, 560, 215, "http://wwww.purebasic.com") 
;   ButtonGadget(#Button_Start, 20, 250, 110, 40, "Start") 
;   ButtonGadget(100, 120, 250, 110, 40, "Edit") 
;   ButtonGadget(200, 220, 250, 110, 40, "Bold") 
  ButtonGadget(300, 320, 250, 110, 40, "GetHTML")
  
  ;hwnd = FindWindow_("IEFrame",0)
  ;browser = COMate_WrapCOMObject(GetWindowLongPtr_(hwnd, #GWL_USERDATA))
  
  browser = COMate_WrapCOMObject(GetWindowLongPtr_(GadgetID(#Web_Gadget), #GWL_USERDATA)) 
  document = browser\GetObjectProperty("Document") 

  ;Set the 'global' event handler for the browser object.
  ;In order to over-ride COMatePLUS' selection of 'connection point' we pass a pointer to the IID of the DWebBrowserEvents2 out-going interface
  ;within the second optional parameter.
    If COMate_GetIIDFromName("DWebBrowserEvents2", @iid.IID) = #S_OK
;      browser\SetEventHandler(#COMate_CatchAllEvents, @EventProc(), 0, iid)
    EndIf
EndIf 


Repeat 
  EventID = WaitWindowEvent() 
  Select EventID
    Case #PB_Event_Gadget 
      Select EventGadget()
        Case 300
;          a$ = GetGadgetItemText(#Web_Gadget, #PB_Web_HtmlCode,#PB_Web_HtmlCode)
a$ = document\GetStringProperty("body\InnerHTML")
          MessageRequester("", a$)
;         Case 200
;           document\Invoke("execCommand('bold', #False, #Void)")
;         Case 100
;           document\SetProperty("DesignMode = 'on'")
; Debug Comate_GetLastErrorDescription()
;         Case #Button_Start 
;          browser\Invoke("Navigate('http://www.purebasic.com/')") 
      EndSelect
  EndSelect
Until EventID = #PB_Event_CloseWindow 

browser\Release() 

End 


Dernière modification par Christophe le mer. 20/févr./2019 13:04, modifié 1 fois.
kwandjeen
Messages : 204
Inscription : dim. 16/juil./2006 21:44

Re: Lire le code HTML d'une page IE

Message par kwandjeen »

httprequest renvoie la page il me semble.
Avatar de l’utilisateur
djes
Messages : 4252
Inscription : ven. 11/févr./2005 17:34
Localisation : Arras, France

Re: Lire le code HTML d'une page IE

Message par djes »

Il veut récupérer la page d'un site ouvert par l'application internet explorer, en gros piloter internet explorer, plutôt que de télécharger directement la page je pense... C'est ça ?
Christophe
Messages : 43
Inscription : lun. 22/oct./2018 19:34
Localisation : Haisnes (62)

Re: Lire le code HTML d'une page IE

Message par Christophe »

Tout à fait djes c'est bien ça... Au début je pensai récupérer la page dans le cache de IE mais je ne peux pas car le chemin d'accès est protégé par un mot de passe que bien évidemment je n'ai pas
boby
Messages : 261
Inscription : jeu. 07/juin/2007 22:54

Re: Lire le code HTML d'une page IE

Message par boby »

Regarde du coté de L'API IE.

PS : IE est abandonné par M$.
Avatar de l’utilisateur
djes
Messages : 4252
Inscription : ven. 11/févr./2005 17:34
Localisation : Arras, France

Re: Lire le code HTML d'une page IE

Message par djes »

Pour le reste (récupérer l'handle d'un programme), tu peux te baser sur ça :

Code : Tout sélectionner

; Author: Danilo
; Date: 07. April 2003
; Modif : 2. July 2008 by djes


; Posted 07 April 2003 by Danilo on german forum

;SendKeys(handel,Window$,Keys$)

;Handel ......... ist ein Handel zu einen Fenster bekommst du wenn du das Fenster erzeugst.
;Window$ ........ Ist der Name des Fesnters, wenn du den Handel nicht kennst. In dem Fall gibt man dan Handel = 0 an.
;Keys$ .......... Sind die Tasten die du schicken möchtest, wobei du Sondertasten in {} Einschliest (zb. {CONTROLDOWN})

; SendKeys procedure by PB -- do whatever you want with it. :)
; Syntax: r=SendKeys(handle,window$,keys$) ; r = 0 for failure.
; Specify either a handle or window$ title to type to, but not both!
;

;
Declare  SendKeys(handle,window$,keys$)

m$= "This example types some text to Notepad, then clears and closes it."+Chr(13)
m$=m$+"Open a new Notepad window, then click the OK button and watch! :)"
MessageRequester("SendKeys example",m$,#MB_ICONINFORMATION)
;
RunProgram("notepad.exe","","",0)
;w$="Sans titre - Bloc-notes" ; Specifies target window name.
w$=""
Delay(1000) : SendKeys(0,w$,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\\\///___----[[[####{{{~~~&&&&|||^^^@@@@]]]]===))))") ; Type some dummy text.
Delay(1000) : SendKeys(0,w$,"{CONTROLDOWN}a{CONTROLUP}")                                                                               ; Select it all.
Delay(1000) : SendKeys(0,w$,"{DELETE}")                                                                                                ; Delete it.

;Delay(1000) : SendKeys(0,w$,"{CONTROLDOWN}p{CONTROLUP}")

Delay(1000) : SendKeys(0,w$,"{ALTDOWN}{F4}") ; Close the Notepad Window


; ****************************************************************************************
; ****************************************************************************************
; ************************ PROCEDURE ****************************************************
; ****************************************************************************************
; ****************************************************************************************



Procedure SendKeys(handle,window$,keys$)
  If window$<>"" 
    handle=FindWindow_(0,window$)  ; Use window$ instead of handle.
  Else
    handle = GetForegroundWindow_()
    name$ = Space(1024)
    GetWindowText_(handle, @name$, 1024)
    Debug name$
  EndIf
  If IsWindow_(handle)=0                                 ; Does the target window actually exist?
    Debug "failed"
    ProcedureReturn 0 ; Nope, so report 0 for failure to type.
  Else
    ; This block gives the target window the focus before typing.
    thread1=GetWindowThreadProcessId_(GetForegroundWindow_(),0)
    thread2=GetWindowThreadProcessId_(handle,0)
    If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf
    SetForegroundWindow_(handle) ; Target window now has the focus for typing.
    Delay(125)                   ; 1/8 second pause before typing, to prevent fast CPU problems.
                                 ; Now the actual typing starts.
    For r=1 To Len(keys$)
      vk$=Mid(keys$,r,1)
      If vk$="{" ; Special key found.
        s=FindString(keys$,"}",r+1)-(r+1) ; Get length of special key.
        s$=Mid(keys$,r+1,s)               ; Get special key name.
        notakey=#False
        Select s$ ; Get virtual key code of special key.
          Case "ALTDOWN" : alted = 1 : keybd_event_(#VK_MENU,0,0,0) ; Hold ALT down.
          Case "ALTUP" : alted = 0 : keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0) ; Release ALT.
          Case "BACKSPACE" : vk=#VK_BACK
          Case "CONTROLDOWN" : ctrled = 1 : keybd_event_(#VK_CONTROL,0,0,0) ; Hold CONTROL down.
          Case "CONTROLUP" : ctrled = 0 : keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0) ; Release CONTROL.
          Case "DELETE" : vk=#VK_DELETE
          Case "DOWN" : vk=#VK_DOWN
          Case "END" : vk=#VK_END
          Case "ENTER" : vk=#VK_RETURN
          Case "F1" : vk=#VK_F1
          Case "F2" : vk=#VK_F2
          Case "F3" : vk=#VK_F3
          Case "F4" : vk=#VK_F4
          Case "F5" : vk=#VK_F5
          Case "F6" : vk=#VK_F6
          Case "F7" : vk=#VK_F7
          Case "F8" : vk=#VK_F8
          Case "F9" : vk=#VK_F9
          Case "F10" : vk=#VK_F10
          Case "F11" : vk=#VK_F11
          Case "F12" : vk=#VK_F12
          Case "ESCAPE" : vk=#VK_ESCAPE
          Case "HOME" : vk=#VK_HOME
          Case "INSERT" : vk=#VK_INSERT
          Case "LEFT" : vk=#VK_LEFT
          Case "PAGEDOWN" : vk=#VK_NEXT
          Case "PAGEUP" : vk=#VK_PRIOR
          Case "PRINTSCREEN" : vk=#VK_SNAPSHOT
          Case "RETURN" : vk=#VK_RETURN
          Case "RIGHT" : vk=#VK_RIGHT
          Case "SHIFTDOWN" : shifted=1 : keybd_event_(#VK_SHIFT,0,0,0) ; Hold SHIFT down.
          Case "SHIFTUP" : shifted=0 : keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0) ; Release SHIFT.
          Case "TAB" : vk=#VK_TAB
          Case "UP" : vk=#VK_UP
          Default
            notakey = #True
        EndSelect
        If notakey=#False
          If Left(s$,3)<>"ALT" And Left(s$,7)<>"CONTROL" And Left(s$,5)<>"SHIFT"
            keybd_event_(vk,0,0,0) : keybd_event_(vk,0,#KEYEVENTF_KEYUP,0) ; Press the special key.
          EndIf
          r=r+s+1 ; Continue getting the keystrokes that follow the special key.
        Else
          vk=VkKeyScanEx_(Asc(vk$),GetKeyboardLayout_(0)) ; Normal key found
          keybd_event_(vk,0,0,0) : keybd_event_(vk,0,#KEYEVENTF_KEYUP,0) ; Press the normal key.
        EndIf
      Else
        ;LoadKeyboardLayout_("000040c",#KLF_ACTIVATE|#KLF_REORDER)
        vk=VkKeyScanEx_(Asc(vk$),GetKeyboardLayout_(0)) ; Normal key found
        If (vk&$0100)<>0 And shifted = 0 : keybd_event_(#VK_SHIFT,0,0,0) : EndIf ; Due to shifted character.
        If (vk&$0200)<>0 And ctrled = 0 :  keybd_event_(#VK_CONTROL,0,0,0) : EndIf
        If (vk&$0400)<>0 And alted = 0 :   keybd_event_(#VK_MENU,0,0,0) : EndIf
        keybd_event_(vk,0,0,0) : keybd_event_(vk,0,#KEYEVENTF_KEYUP,0) ; Press the normal key.
        If (vk&$0100)<>0 And shifted = 0 : keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0) : EndIf ; Due to shifted character.
        If (vk&$0200)<>0 And ctrled = 0  : keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0) : EndIf
        If (vk&$0400)<>0 And alted = 0   : keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0) : EndIf
      EndIf
    Next
    
    If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#False) : EndIf ; Finished typing to target window!
    keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0)                              ; Release ALT key if user forgot.
    keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0)                           ; Release CONTROL key if user forgot.
    keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0)                             ; Release SHIFT key if user forgot.
    ProcedureReturn 1                                                        ; Report successful typing! :)
  EndIf
EndProcedure 
Marc56
Messages : 2147
Inscription : sam. 08/févr./2014 15:19

Re: Lire le code HTML d'une page IE

Message par Marc56 »

Christophe a écrit :Tout à fait djes c'est bien ça... Au début je pensai récupérer la page dans le cache de IE mais je ne peux pas car le chemin d'accès est protégé par un mot de passe que bien évidemment je n'ai pas
Cela veut dire que ce n'est pas le cache de ta session IE, mais celui d'un autre utilisateur.
Il est alors normal que l'accès te soit refusé à moins d'avoir un compte admin.
Le cache de ton IE est dans ton profile local. %appdata%\Local

... AppData\Local\Microsoft\Windows\INetCache\

Pour le connaitre:
- Win + R
- iexplorer
- options internet
- onglet 1: général
- Rubrique: historique de navigation
- paramètres
- Emplacement actuel
Christophe
Messages : 43
Inscription : lun. 22/oct./2018 19:34
Localisation : Haisnes (62)

Re: Lire le code HTML d'une page IE

Message par Christophe »

Merci pour votre aide j'avance doucement... Maintenant je récupere bien le code de la page

Marc56, effectivement je ne suis pas l'admin
boby, c'est vrai que je n'avais pas pensé à l'API IE j'y ai trouvé des infos !
djes, je pense avoir trouvé comment récuperer mon Handle par ce programme

Code : Tout sélectionner

Procedure.l EnumProcedure(WindowHandle.l, Parameter.l)
  Protected cTitle.s = Space(200)
  Protected cClass.s = Space(200)
  GetWindowText_(WindowHandle, @cTitle, 200)
  GetClassName_(WindowHandle, @cClass,200)
  Debug  Str(WindowHandle) + " : " + cTitle + "  :  " + cClass
  ProcedureReturn  #True
EndProcedure



hwnd = FindWindow_("IEFrame",0)
EnumChildWindows_(hwnd,@EnumProcedure(), 0)
        


Le problème est que lorsque je lance le programme il ouvre une nouvelle page IE du au fait de la fonction 'browser = COMate_CreateObject("InternetExplorer.Application")'
quelqu'un aurait il une idée pour me servir de la page deja existante ? si j'ai l'handle ou le mettre ?

voici l'avancement du programme

Code : Tout sélectionner

;***COMate***  COM automation through iDispatch.
;*===========
;*
;*Web gadget demo. (Advanced users!)  Based upon code by hm.
;*
;*This demo not only shows how to obtain a COMateObject from a web-gadget, but also sets a 'global' event handler.
;*
;*This demo also shows how to over-ride the COMateObject SetEventHandler() method and direct COMate to seek out a specific
;*'out-going interface'. Left to it's own devices, COMatePLUS will utilise the first suitable 'connection point' and out-going interface
;*that it encounters. In the case of a web gadget, we direct COMatePLUS to directly seek out a connection point for the newer DWebBrowserEvents2
;*interface.

;*I did state that this demo is for advanced users!
;/////////////////////////////////////////////////////////////////////////////////


IncludePath "..\"
XIncludeFile "COMatePLUS.pbi"

Global browser.COMateObject
Global document.COMateObject
Global frames.COMateObject

Global eventName$
Declare GetHtml()




;/////////////////////////////////////////////////////////////////////////////////
;The following is our event callback for our web-gadget object.
;The 'Object' parameter will contain the COMate object upon which was used the \SetEventHandler() method; in our case it will point
;to our browser object.
Procedure EventProc(Object.COMateObject, eventName$, parameterCount) 
  Debug eventName$
  
EndProcedure 
;/////////////////////////////////////////////////////////////////////////////////


Procedure GetHtml()
  document = browser\GetObjectProperty("Document") : Debug "document :"+document
  If document
    frames = document\GetObjectProperty("frames") : Debug "frames :"+frames 
    If frames
      Debug document\GetStringProperty("Head\InnerHtml")
      Debug document\GetStringProperty("Body\InnerHtml")
    EndIf
  EndIf
  
EndProcedure


If OpenWindow(0, #PB_Ignore, #PB_Ignore, 130, 60, "WebGadget Events Test",  #PB_Window_SystemMenu | #PB_Window_TitleBar ) 
  StickyWindow(0,1)
  ButtonGadget(300, 10, 10, 110, 40, "GetHTML")
  
  ;hwnd = FindWindow_("IEFrame",0)
  ;browser = COMate_WrapCOMObject(GetWindowLongPtr_(hwnd, #GWL_USERDATA))
  
  browser = COMate_CreateObject("InternetExplorer.Application")
  browser\SetProperty("Visible = #true")
  browser\invoke("Navigate('http://www.purebasic.com')")
  
  
  
  ;Set the 'global' event handler for the browser object.
  ;In order to over-ride COMatePLUS' selection of 'connection point' we pass a pointer to the IID of the DWebBrowserEvents2 out-going interface
  ;within the second optional parameter.
  
  If COMate_GetIIDFromName("DWebBrowserEvents2", @iid.IID) = #S_OK
    browser\SetEventHandler(#COMate_CatchAllEvents, @EventProc(), 0, iid)
  EndIf
EndIf


Repeat
Until browser\GetIntegerProperty("ReadyState") = 4




If browser 
  document = browser\GetObjectProperty("Document") : Debug "document :"+document
  If document
    frames = document\GetObjectProperty("frames") : Debug "frames :"+frames
    If frames
      Debug document\GetStringProperty("Head\InnerHtml")
      Debug document\GetStringProperty("Body\InnerHtml")
    EndIf
  EndIf
EndIf


Repeat 
  EventID = WaitWindowEvent() 
  
  Select EventID
    Case #PB_Event_Gadget 
      Select EventGadget()
        Case 300
          GetHtml()
      EndSelect
  EndSelect
  
Until EventID = #PB_Event_CloseWindow

frames\Release()

document\Release()

browser\Release()








Lien pour les personnes n'ayant pas COMatePlus
https://www.rsbasic.de/backupprogramme/COMatePLUS.zip
Avatar de l’utilisateur
djes
Messages : 4252
Inscription : ven. 11/févr./2005 17:34
Localisation : Arras, France

Re: Lire le code HTML d'une page IE

Message par djes »

Peut-être que tu peux retrouver l'objet plus facilement avec ce code

Code : Tout sélectionner

XIncludeFile "COMatePLUS.pbi"

Define.COMateObject objWMIService, objProcess
colProcessList.COMateEnumObject
strComputer.s = "."

objWMIService = COMate_GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + strComputer + "\root\cimv2")
If objWMIService
  colProcessList = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_Process')")
  
  If colProcessList
    objProcess = colProcessList\GetNextObject()
    While objProcess
      
      Process_name$ = objProcess\GetStringProperty("Name")
      Process_Id$ = Str(objProcess\GetIntegerProperty("ProcessId"))
      Process_handle$ = Str(objProcess\GetIntegerProperty("Handle"))
      Executable_Path$ = objProcess\GetStringProperty("ExecutablePath")
      Debug Executable_Path$
      
      objProcess\Release()
      objProcess = colProcessList\GetNextObject()
    Wend
    colProcessList\Release()
  EndIf
  objWMIService\Release()
Else
  MessageRequester("Error", "ProcInfo")
EndIf
Christophe
Messages : 43
Inscription : lun. 22/oct./2018 19:34
Localisation : Haisnes (62)

[Résolu] Lire le code HTML d'une page IE

Message par Christophe »

Solution de mon problème résolu par mk-soft

Code : Tout sélectionner

;-TOP

; Comment   : Modul ActiveScript Example 7

; Link to ActiveScript  : https://www.purebasic.fr/english/viewtopic.php?f=12&t=71399
; Link to SmartTags     : https://www.purebasic.fr/english/viewtopic.php?f=12&t=71399#p527089
; Link to VariantHelper : https://www.purebasic.fr/english/viewtopic.php?f=12&t=71399#p527090

; ***************************************************************************************

XIncludeFile "Modul_ActiveScript.pb"
;XIncludeFile "Modul_SmartTags.pb"
;XIncludeFile "VariantHelper.pb"

UseModule ActiveScript
;UseModule ActiveSmartTags

; -------------------------------------------------------------------------------------

Global vbs.s, result.s
Global title.s
; Variable als Rumtime definieren zum Zugriff aus VB-Script
Runtime result
Runtime title

Title = "PureBasic"

; Daten anlegen

; VB-Script schreiben

vbs + #LF$ + ~" On Error Resume Next" 
vbs + #LF$ + ~" "
vbs + #LF$ + ~" Dim w, sWinTitle, oShell, oWindow"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" Set oShell = CreateObject(\"shell.application\")"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" sWinTitle = Runtime.String(\"Title\")"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" For Each w in oShell.Windows"
vbs + #LF$ + ~"   If InStr(1, typename(w.document),\"htmldocument\", 1) > 0 Then"
vbs + #LF$ + ~"     If InStr(1, w.document.title, sWinTitle, 1) = 1 Then "
vbs + #LF$ + ~"       Set oWindow = w"
vbs + #LF$ + ~"       Runtime.String(\"Title\") = w.document.title"
vbs + #LF$ + ~"     End If"
vbs + #LF$ + ~"   End If"
vbs + #LF$ + ~" Next"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" Set oShell = Nothing"
vbs + #LF$ + ~" "
vbs + #LF$ + ~" If Not IsObject(oWindow) then "
vbs + #LF$ + ~"   Runtime.String(\"result\") = \"InternetExplorer: Page not found\""
vbs + #LF$ + ~" Else"
vbs + #LF$ + ~"   Runtime.String(\"result\") = oWindow.document.documentElement.outerHTML"
vbs + #LF$ + ~"   Set oWindow = Nothing"
vbs + #LF$ + ~" End If"
vbs + #LF$ + ~" "
If NewActiveScript()
  Debug "************************************************************"
  Debug vbs
  Debug "************************************************************"
  ParseScriptText(vbs);, #SCRIPTTEXT_ISVISIBLE)
  FreeActiveScript()
  Debug "************************************************************"
  Debug title
  Debug result
EndIf
:D
Répondre