www.webdeveloper.com
Results 1 to 3 of 3

Thread: how to extract images from web page given a URL?

  1. #1
    Join Date
    May 2012
    Posts
    1

    how to extract images from web page given a URL?

    I would like to submit a url and extract the images of this web page submitted. How to do this using javascript? If anyone knows I ask that you create a basic script that I can understand.


    Thanks,

    Marcelo
    Rio - Brazil

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    it's easy with yql:


    Code:
    function addScript(u){var sc2=document.createElement('script'); sc2.src=u;
      return document.getElementsByTagName('*')[1].appendChild(sc2);
    }
    
    
    
    function grabImgs(url, cb){
     var url="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22"+
       encodeURIComponent(url)+
       "%22%20and%0A%20%20%20%20%20%20xpath%3D'%2F%2Fimg'&format=json&callback="+
       ( (cb && cb.name) || cb ||"console.log");
    
     return  addScript(url);
    
    }
    
    function myCB(data){
      alert(JSON.stringify(   data.query.results.img    ,null,"\t"))
    }
    
    //demo
    grabImgs("http://www.webdeveloper.com/forum/showthread.php?t=260425", myCB)
    Last edited by rnd me; 05-11-2012 at 12:16 AM.

  3. #3
    Join Date
    Oct 2006
    Posts
    939
    i guess i can understand that. But my interpretation doesn't work with IE
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    	"http://www.w3.org/TR/html4/strict.dtd">
    <HTML>
    <HEAD><TITLE>Sa12-17pm37</TITLE>
    <META http-equiv=Content-Type content="text/html; charset=UTF-8">
    <STYLE type=text/CSS>
    
    </STYLE>
    <SCRIPT type="text/javascript">
    
    </SCRIPT>
    <META content="MSHTML 6.00.2900.2963" name=GENERATOR></HEAD>
    <BODY>
    <div>
    <SCRIPT type="text/javascript">
    var pcs='cute,Pictures,topwalls,wallpapers,wallpaper,creepy,EarthPorn,photos,pics,mfw,gif,gifs,reactiongifs';
    	pcs=pcs.split(',')
    
    var po='http://imgur.com/r/'	
    for(i=0;i<pcs.length;i++){document.write('<span onclick="grabImgs(\'http://imgur.com/r/'+pcs[i]+'\',myCB);this.style.backgroundColor=\'pink\'" style="color:blue;cursor:pointer">'+pcs[i]+'</span> ')}
    </SCRIPT>
    </div><div id="pics" style="text-align:center;min-height:500px"></div>
    <SCRIPT type="text/javascript">
    var imgu='http://imgur.com/r/'
    function addScript(u){var sc2=document.createElement('script'); sc2.src=u;
    	  return document.getElementsByTagName('*')[1].appendChild(sc2);
    }
    
    function grabImgs(url, cb){
      while(document.getElementById('pics').hasChildNodes()){document.getElementById('pics').removeChild(document.getElementById('pics').childNodes[0]);}
     var url="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22"+
       encodeURIComponent(url)+
       "%22%20and%0A%20%20%20%20%20%20xpath%3D'%2F%2Fimg'&format=json&callback="+
       ( (cb && cb.name) || cb ||"console.log");
     return  addScript(url);
    }
    
    function myCB(data){
      var k=JSON.stringify(   data.query.results.img    ,null,null)
    	var n=k=k.split('"src":"'); k.splice(0,1);n.splice(0,1);
    	for(i=0;i<k.length;i++){	scr=k[i].substring(0,k[i].indexOf('"')); bgi=scr.substring(0,scr.length-5); var ch=scr.substring(scr.length-5,scr.length-4);
    		if(ch=='b'){k[i]='<img src="'+scr+'" title="'+ch+'" onclick="seeBig(\''+bgi+'\')" style="cursor:pointer;margin-top:0px;margin-left:0px;margin-bottom: 0px;margin-right: 1px">'}
    
    		else{k[i]='' }}
    	document.getElementById('pics').innerHTML=k.join('')
    }
    function seeBig(d){
    	window.open(d+'.jpg',Math.random()+'aWin','') 
    }
    
    </SCRIPT>
    
    </BODY></HTML>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles