Finding the names of blogs on hypemachine that might feature bands you like.


If you have always wanted to web scrape - here is the example for you, this is based on one of the Perl gods's modules: Andy Lester's WWW::MECHANIZE. I use this module for a lot of web parsing and text mining - everything from blogs, to fashion pictures
to ophthalmologists.


#!/usr/bin/perl

use strict ;
use WWW::Mechanize;


open(TEXT,"+>hypemachine.txt");
open(TEXTe,"+>hypemachine-electrolinks.txt");

print TEXTe "All the blogs on hypemachine\n\n" ;

my $mech = WWW::Mechanize->new();

# make an array of browsers to trick google with
$mech->agent_alias( 'Windows IE 6' );

#------------------------------
# Manage cookies

my $response = $mech->get( "http://hypem.com/list" );

my @linksfound = $mech->find_all_links( url_regex => qr/http:\/\// );



print "response gotten\n" ;

foreach (@linksfound) {

       my $fs = $_->url(), "\n";
    print "got $fs\n" ; 

    #print TEXT $_->url(), "\n" ;

    $mech->get( $fs );
    print "following $fs\n" ; 
  
  my $content = $mech->content() ;
  
  $content =~ s/'\n'/' '/g ;
  print $content ;
   while ( $content=~ m/(mstrkrft|daft punk|crookers|ed banger|Justice|chromeo|treasure fingers)/g   ) 
    {
    $content =~m/<title>(.*)<\/title>/;
    print TEXTe "site contains Electro\t$fs\t$1\t$2\n" ; 
    
   
   } 

       

}

Comments

Popular posts from this blog

Find countries in a file's text using regular expressions and Perl

Make links active in Excel 2007