Advertisement
HwapX

Desafio Navegando pela arvore de categoria/subcategoria do FI

Nov 1st, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.25 KB | None | 0 0
  1. ## http://www.forum-invaders.com.br/vb/showthread.php/41212-Desafio-08-Perl-Navegando-pela-arvore-de-categoria-subcategoria-do-FI
  2.  
  3. #!/usr/bin/perl -w
  4. use strict;
  5. use warnings;
  6. use LWP::Simple;
  7. use List::MoreUtils qw(uniq);
  8.  
  9. sub walk {
  10.     my $uri = $_[0];
  11.     my $url = $uri.$_[1];
  12.     $url =~ s/\?s=.+$//;
  13.     my $level = $_[2];
  14.     my $content = get($url);
  15.     my @urls = $content =~ /<h2 class="forumtitle"><a href="(.+?)">/g;
  16.     my @categories = $content =~ /<h2 class="forumtitle"><a href=".+?">(.+?)<\/a>/g;
  17.    
  18.     @_ = ();
  19.     foreach (@categories) {
  20.         #print "\t"x$level .$_, "\n";
  21.         push(@_, "\t"x$level.$_);
  22.         push(@_, walk($uri, shift @urls, $level + 1));
  23.     }
  24.  
  25.     my $pages = 1;
  26.     $pages = $1 if($content =~ /PΓ‘gina 1 de (.+?)</);
  27.    
  28.     my @topics = $content =~ /<a class="title.+?>(.+?)<\/a>/g;
  29.     push(@topics, (get("$url/page$_") =~ /<a class="title.+?>(.+?)<\/a>/g)) for (2..$pages);
  30.     push(@_, map "\t"x$level.$_, uniq(@topics));#Usado para remover os topicos fixos repetidos
  31.     #porem podem existir topicos normais com o mesmo titulo porem com conteudo distinto
  32.  
  33.     return(@_);
  34. }
  35.  
  36. open (FH, ">", "wtf.txt");
  37. print FH join("\n", walk("http://www.forum-invaders.com.br/vb/", "",  0));
  38. close FH;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement