2014년 6월 3일 화요일

get naver kin

use LWP::UserAgent;
use autodie qw/ open close /;
use WWW::Mechanize;
use LWP::Simple qw($ua get);
use Encode;

$filename="naverkin-phy.txt";
if(-e $filename)
    {
        unlink($filename);
    }

$ua->agent('My agent/1.0');

# create WWW::Mechanize object
# autocheck 1 checks each request to ensure it was successful
my $mech = WWW::Mechanize->new();

$mech->get("http://kin.naver.com/qna/list.nhn?m=noanswer&dirId=1114&page=1");
#$no=197187698;


$list_cont = $mech->content();

$list_cont=encode("euc-kr", $list_cont);
#print $list_cont;

@link = split(/docId=(\d{9})/, $list_cont);


for ($i=1; $i<=$#link; $i++){
    if ($i%2 == 1){
     push(@no, $link[$i]);
    }
}

@no = sort {$a <=> $b} @no;  # 1,2,3 순으로 정렬; Ascending
 
for ($i=1; $i<=$#no; $i++){

print $no[$i]." article is extracted...\n";

# paste from url of webbrowser manually later modify
$mech->get("http://kin.naver.com/qna/detail.nhn?d1id=11&dirId=1114&docId=$no[$i]");

$cont = $mech->content();
$cont=encode("euc-kr", $cont);

#@temp = split(/og:url/, $cont);
#@temp = split(/science&no=/, $temp[1]);
#@temp = split(/&page/, $temp[1]);


@temp = split(/tit_cont/, $cont);
@temp = split(/recom_q/, $temp[1]);
@temp = split(/img/, $temp[0]);
@temp = split(/dl/, $temp[0]);

$subject = $temp[0];
$subject =~ s/">//g;
$subject =~ s/h3>//g;
$subject =~ s/<//g;
$subject =~ s/\///g;
$subject =~ s/^\s+//g;
$subject =~ s/\s+$//g;

$subject = "subject: ".$subject;


@temp = split(/tit_cont/, $cont);
@temp = split(/recom_q/, $temp[1]);
@temp = split(/<dl>/, $temp[0]);
@temp = split(/<\/dl>/, $temp[1]);
@temp = split(/<dt>/, $temp[0]);
@temp = split(/<\/dt>/, $temp[1]);

$name = $temp[0];

$name =~ s/^\s+//g;
$name =~ s/\s+$//g;
$name = "name: ".$name;


@temp = split(/본문/, $cont);
@temp = split(/게시중단 안내/, $temp[3]);
$out = $temp[1];
$out =~ s/!<--$//;
$out =~ s/<\/div>//;
$out =~ s/\!<--//;
$memo = $out;

#print $memo;

$memo = "memo: ".$memo;

@temp = split(/본문/, $cont);
@temp = split(/게시중단 안내/, $temp[3]);
@img_url = split(/src="/,$temp[1]);
@img_url = split(/\?type/,$img_url[1]);
#print $img_url[0];

$img_url[0]=~ m/jpg|png|JPG|PNG/;

if ($img_url[0]){
$img_type = $&;
$mech->mirror($img_url[0], $no.".".$img_type);
}

$attached = "file:".$no.".".$img_type;

open MYFILE, ">>", "naverkin-phy.txt" or die ;
 print MYFILE "$no[$i]";
 print MYFILE "------------------------------------------------------------------------------\n";
 print MYFILE "$name\n";
 print MYFILE "$subject\n";
 print MYFILE "$memo\n";
 print MYFILE "$attached\n";

}

get bgm from bgmstore

use LWP::UserAgent;
use autodie qw/ open close /;
use WWW::Mechanize;
use LWP::Simple qw($ua get);
use Encode;

$ua->agent('My agent/1.0');

# create WWW::Mechanize object
# autocheck 1 checks each request to ensure it was successful
my $mech = WWW::Mechanize->new();

if (-e "bgmstore.txt"){
    unlink("bgmstore.txt");
    }
   
$mech->get("http://bgmstore.net/?q_type=category&q=%EC%9C%A0%EB%A8%B8");
@link = $mech->find_all_links( url_regex => qr/view/);

for $new_link (@link){
$url = $new_link->url_abs();

$no++;

if ($no % 2 == 0 ){
print $url;

$mech->get($url);
$cont = $mech->content();

print $no." article is extracted...\n";
@files=();

$cont=encode("euc-kr", $cont);

@temp = split(/titleBox/, $cont);
@temp = split(/<span/, $temp[1]);
$subject = $temp[0];
$subject =~ s/clearfix">//;

print $subject;
$name = "땡초";

@mp3 = $mech->find_all_links( url_regex => qr/mp3/);

print $mp3[0]->url_abs();
$filename = $subject;
$filename =~ s/,//g;
$filename =~ s/\?//g;
$filename =~ s/\s+//g;
print $filename;

$mech->mirror($mp3[0]->url_abs(), $filename.".mp3");


$name = "name: ".$name;
$subject = "subject: ".$subject;
$memo = "memo: ".$memo;

open MYFILE, ">>", "bgmstore.txt" or die ;
 print MYFILE "$no";
 print MYFILE "------------------------------------------------------------------------------\n";
 print MYFILE "$name\n";
 print MYFILE "$subject\n";
 print MYFILE "$memo\n";
 }
}

2014년 5월 30일 금요일

우분투에서 루트 권한으로 명령 실행

우분투에서 루트 권한으로 명령을 실행하기 위해서는 root password가 설정되어 있어야 한다.

sudo passwd root

이후 sudo 명령을 사용할 수 있다

윈도우에 git 클라이언트 설치

다운로드
http://git-scm.com/download/win

path에 설치폴더의 바이너리 경로를 등록

명령프롬프트에서 실행하면 된다

perl mechanize example

use LWP::UserAgent;
use autodie qw/ open close /;
use WWW::Mechanize;
use LWP::Simple qw($ua get);
use Encode;


$ua->agent('My agent/1.0');

# create WWW::Mechanize object
# autocheck 1 checks each request to ensure it was successful
my $mech = WWW::Mechanize->new();

$filename = "ilbe.txt";
if(-e $filename)
    {
        unlink($filename);
    }
   
$mech->get("http://www.ilbe.com/ilbe");

@link = $mech->find_all_links( url_regex => qr/\d{10}/);

for $new_link (@link){

$url = $new_link->url_abs();

$no = $url;
$no =~ m/\d{10}/;

$no = $&;

$mech->get($url);
$cont = $mech->content();

print $no." article is extracted...\n";
@files=();

# paste from url of webbrowser manually later modify

#@temp = split(/og:url/, $cont);
#@temp = split(/science&no=/, $temp[1]);
#@temp = split(/&page/, $temp[1]);


#$no = $temp[0];

$cont=encode("euc-kr", $cont);

@temp = split(/author/, $cont);
@temp = split(/<\/div>/, $temp[1]);
@temp = split(/<\/div>/, $temp[0]);
@temp = split(/margin-right:3px;/, $temp[0]);
$name = $temp[1];
$name =~ s/"//;
$name =~ s/\/>//;
$name =~ s/^\s+//;
@temp = split(/<\/span>/, $name);
$name =~ s/\s+$//;
$name = $temp[0];
#print $name;

@temp = split(/기사 제목/, $cont);
@temp = split(/-->/, $temp[1]);
@temp = split(/<!--/, $temp[1]);

$subject = $temp[0];

print $subject;


@temp = split(/기사 본문/, $cont);
@temp = split(/serverLog/, $temp[1]);

$memo = $temp[0];
$memo =~ s/-->//;
$memo =~ s/^\s+//;

#print $memo;



@img = $mech->find_all_links( text_regex => qr/png/i );

$i = 0;
for my $img_url (@img) {
    $i++;
    print $img_url->url_abs;
    $mech->mirror($img_url->url_abs, $no."-".$i.".png");
    push (@files, $no."-".$i.".png");
   
}

@img = $mech->find_all_links( text_regex => qr/jpg/i );
for my $img_url (@img) {
    $i++;
    print $img_url->url_abs;
    $mech->mirror($img_url->url_abs, $no."-".$i.".jpg");
    push (@files, $no."-".$i.".jpg");
   
    #Opens FROM_DIR, ao I can read from it
   
       
}

$attached = "file:";
for ($i=0; $i<=$#files; $i++){
$attached = $attached.$files[$i]." ";

}

$name = "name: ".$name;
$subject = "subject: ".$subject;
$memo = "memo: ".$memo;


open MYFILE, ">>$filename" or die ;
 print MYFILE "$no";
 print MYFILE "------------------------------------------------------------------------------\n";
 print MYFILE "$name\n";
 print MYFILE "$subject\n";
 print MYFILE "$memo\n";
 print MYFILE "$attached\n";
}

2014년 5월 23일 금요일

펄 모듈 설치 모듈이 서브 타입으로 되어 있을 때

모듈이름이
WWW::Mechanize::Firefox

다 써주어야 한다.

cpan install WWW::Mechanize::Firefox

대소문자도 구분하니까 유의

2014년 5월 8일 목요일

하위 폴더의 모든 파일에서 특정 문자열을 검색하는 펄스크립트

use strict;
use warnings;
use Cwd;

use File::Find;

my $search_pattern=$ARGV[0];
my $file_pattern  =$ARGV[1];

if (@ARGV <2){
print "Useage: $0 [string to search] [file type]\n";
exit 1;
}

find(\&d, cwd);

sub d {

  my $file = $File::Find::name;

  $file =~ s,/,\\,g;

  return unless -f $file;
  return unless $file =~ /$file_pattern/;

  open F, $file or print "couldn't open $file\n" && return;

  while (<F>) {
    if (my ($found) = m/($search_pattern)/o) {
      print "found $found in $file\n";
      last;
    }
  }

  close F;
}