Macro récupérant code source site Internet
Bonjour,
Je ne sais pas si cela est faisable et légal (je pense que oui vu c'est à disposition public), mais j'aimerais en VBA pouvoir aller récupérer le code source d'une page Internet.
Par exemple, je lui donne en paramètre cette page là: http://www.jeux.fr/
, et lui me sort en retour:
<!DOCTYPE html>
<!--[if lt IE 8]><html class="lt-ie9 lt-ie8"><![endif]-->
<!--[if IE 8]><html class="lt-ie9 ie8"><![endif]-->
<!--[if IE 9]><html class="ie9"><![endif]-->
<!--[if gt IE 9]><!--><html><!--<![endif]-->
<head>
<meta charset="utf-8"/>
<meta property="portal:site:id" content="11"/>
<meta property="portal:channel:id" content="1"/>
<meta property="sg:type" content="portal"/>
<meta name="viewport" content="width=device-width, maximum-scale=1.0, initial-scale=1.0, user-scalable=no, minimal-ui"/>
<meta name="description" content="Jouez à tous les jeux gratuits sur jeux.fr. Des milliers de jeux en ligne à découvrir pour les enfants et les adultes sur Jeux.fr"/>
<meta name="keywords" content="Jeux en linge, jeux gratuits, jeux gratuits en ligne, jeux des puzzles, jeux d'action, jeux d'aventure, jeux de sport, jeux multijoueurs"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta property="portal:page:type" content="HP"/>
<meta name="google-site-verification" content="UpTjHFxpwHaFEnJ_hjDJ3hhVCu8GORK16YKsn8W01_M"/>
<meta property="og:site_name" content="jeux.fr"/>
<title>Jeux.fr - Jeux Gratuits, Jeux en Ligne, Jeu Gratuit, Jeux Online!</title>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="/wdg/css_aggregator-4.14.5/css/family/theme.css"/>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "http://www.jeux.fr/",
"potentialAction": {
"@type": "SearchAction",
"target": "http://www.jeux.fr/chercher/?searchTerm={search_term_string}",
"query-input": "required name=search_term_string"
}
}
</script>
...
<script>window.lightningjs||function(c){function g(b,d){d&&(d+=(/\?/.test(d)?"&":"?")+"lv=1");c[b]||function(){var i=window,h=document,j=b,g=h.location.protocol,l="load",k=0;(function(){function b(){a.P(l);a.w=1;c[j]("_load")}c[j]=function(){function m(){m.id=e;return c[j].apply(m,arguments)}var b,e=++k;b=this&&this!=i?this.id||0:0;(a.s=a.s||[]).push([e,b,arguments]);m.then=function(b,c,h){var d=a.fh[e]=a.fh[e]||[],j=a.eh[e]=a.eh[e]||[],f=a.ph[e]=a.ph[e]||[];b&&d.push(b);c&&j.push(c);h&&f.push(h);return m};return m};var a=c[j]._={};a.fh={};a.eh={};a.ph={};a.l=d?d.replace(/^\/\//,(g=="https:"?g:"http:")+"//"):d;a.p={0:+new Date};a.P=function(b){a.p[b]=new Date-a.p[0]};a.w&&b();i.addEventListener?i.addEventListener(l,b,!1):i.attachEvent("on"+l,b);var q=function(){function b(){return["<head></head><",c,' onload="var d=',n,";d.getElementsByTagName('head')[0].",d,"(d.",g,"('script')).",i,"='",a.l,"'\"></",c,">"].join("")}var c="body",e=h[c];if(!e)return setTimeout(q,100);a.P(1);var d="appendChild",g="createElement",i="src",k=h[g]("div"),l=k[d](h[g]("div")),f=h[g]("iframe"),n="document",p;k.style.display="none";e.insertBefore(k,e.firstChild).id=o+"-"+j;f.frameBorder="0";f.id=o+"-frame-"+j;/MSIE[ ]+[6|7|8]/.test(navigator.userAgent)&&(f[i]="javascript:false");f.allowTransparency="true";l[d](f);try{f.contentWindow[n].open()}catch(s){a.domain=h.domain,p="javascript:var d="+n+".open();d.domain='"+h.domain+"';",f[i]=p+"void(0);"}try{var r=f.contentWindow[n];r.write(b());r.close()}catch(t){f[i]=p+'d.write("'+b().replace(/"/g,String.fromCharCode(92)+'"')+'");d.close();'}a.P(2)};a.l&&setTimeout(q,0)})()}();c[b].lv="1";return c[b]}var o="lightningjs",k=window[o]=g(o);k.require=g;k.modules=c}({});window.usabilla_live = lightningjs.require("usabilla_live", "//w.usabilla.com/232bf8c2bee0.js");</script>
<!-- 0_031AB1AB -->
</body>
</html>
Ce qui correspond au code source associé. Pour le voir sur Google chrome il suffit de faire clic-droit sur la page Internet puis "Afficher le code source de la page".
Merci d'avance des retours
Bonjour,
une proposition
Sub queryweb()
Set ie = CreateObject("InternetExplorer.application")
ie.Visible = False
qurl = "www.jeux.fr"
ie.navigate qurl
t = Timer
timeout = False
Do
DoEvents
If Timer - t > 50 Then
ie.navigate qurl
Else
If Timer - t > 500 Then MsgBox "time-out": timeout = True: ie.Application.Quit: Exit Do
End If
Loop Until ie.readyState = 4
If Not timeout Then
webtext = ie.document.body.innerHTML
End If
End Sub
Bonjour,
voici une p'tite fonction plus rapide renvoyant le code source d'une page Web :
Function WebPage$(URL$)
With CreateObject("MSXML2.XMLHTTP")
.Open "POST", URL, False
On Error Resume Next
.Send
On Error GoTo 0
If .Status = 200 Then WebPage = .responseText
End With
End Function
C'est si simple que ça?
Je m'attendais à un truc super complexe et en fait pas trop
Merci à vous deux, ça marche bien
Effectivement, utiliser une bibliothèque de requêtes est bien plus simple (et rapide !) que de piloter IE …