iagdotme

Yahoo2CSV

Jul 4th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var s=document.createElement('script');
  2. s.setAttribute('src','http://code.jquery.com/jquery.min.js');
  3. document.getElementsByTagName('body')[0].appendChild(s);
  4.  
  5.  
  6.  
  7.  
  8. function toCSV( rows ){
  9.     var newline = '\n';
  10.     var fsep = ','; // field separator
  11.     var quot = '"'; // field quoter
  12.     var quotquot = [ quot + quot, '\\' + quot ][1]; // choose
  13.     var alwaysQuote = false;
  14.     var quoteIfContainsQuote = true;
  15.     // var trimFields, quoteStringsButNotNumbers(excel), putEqualSignBeforeNumbers(excel), etc.
  16.     var lines = [];
  17.     $.each( rows, function(){
  18.       var line = [];
  19.       $.each( this, function(){
  20.           var s = this;
  21.           var needsQuote = alwaysQuote;
  22.  
  23.           if( s.indexOf( fsep ) >= 0 )
  24.               needsQuote = true;
  25.  
  26.           if( s.indexOf( quot ) >= 0 ){
  27.               needsQuote = needsQuote || quoteIfContainsQuote;
  28.               s = s.replace( quot, quotquot );
  29.           }
  30.  
  31.           if( needsQuote ) s = quot + s + quot;
  32.           line.push( s );
  33.       });
  34.       lines.push( line.join( fsep ));
  35.     });
  36.     return lines.join( newline );
  37. }
  38.            
  39. function xtract( rootTableSelector ){
  40.     var rows = [];
  41.     var csv = [];
  42.     $( rootTableSelector ).each(function(){
  43.         var $t = $(this);
  44.         var row = {};
  45.         row.name  = $.trim( $t.find('tr:nth-child(1) > td > b').text());
  46.         row.email = $.trim( $t.find('tr:nth-child(2) > td > div:nth-child(2)').text());
  47.         rows.push( row );
  48.         csv.push([ row.name, row.email ]);
  49.     });
  50.     console.log( rows );
  51.     $('body').prepend('<pre id="result"></pre>');
  52.     $('img,.qprintable').remove();
  53.     $('#result').text( toCSV( csv ) );
  54.     $('#json').text( JSON.stringify( rows) );
  55.     // prompt('this is not csv', csv );
  56. }
  57. $(function(){
  58.     xtract('.qprintable2');
  59. });
Add Comment
Please, Sign In to add comment