跳至內容
Sars' History
使用者工具
登入
網站工具
工具
顯示頁面
舊版
反向連結
最近更新
多媒體管理器
網站地圖
登入
最近更新
多媒體管理器
網站地圖
足跡:
perl:yahoo_dictionary
本頁是唯讀的,您可以看到原始碼,但不能更動它。您如果覺得它不應被鎖上,請詢問管理員。
====== yahoo 字典 ====== 參考:[[http://plog.longwin.com.tw/programming/2007/01/12/y_dictionary_script_2007]] ===== Perl 版本 ===== <code> #!/usr/bin/perl # Yen-Ming Lee <leeym@leeym.com> # 2007/01/09 use Encode qw(encode decode from_to); use Term::ANSIColor qw(:constants); use Data::Dumper; use LWP::Simple; use strict; ydict(shift); sub ydict { my $p = shift; die if !$p; my $html = get("http://tw.dictionary.yahoo.com/search?p=$p"); from_to($html, "utf-8", "big5") if $ENV{'LC_CTYPE'} =~ /Big5/; my $i = 0; $html =~ s/\r//g; $html =~ s/\n//g; if ($html =~ m{<em class="warning">}) { if ($html =~ m{<em class="warning">.*?>(\S+)<.*?</em>}) { my $q = $1; print BOLD . YELLOW . "\nERROR: $p -> $q\n" . RESET; return ydict($q); } else { print BOLD . YELLOW . "ERROR: $p\n" . RESET; return; } } print BOLD . YELLOW . "\n$p\n" . RESET; while ($html =~ m{<div class=p(\w+)>(.*?)</div>}i) { my $type = $1; my $line = $2; $html = $'; my $color; my $bold = BOLD; my $reset = RESET; $line =~ s/^\s+//; $line =~ s/\s+$//; if ($type eq 'cixin') { $i = 0; $color = BOLD . RED; } elsif ($type eq 'chi' or $type eq 'eng') { $color = CYAN; $reset = RESET . $color; $line = "\t$line"; } elsif ($type eq 'explain') { $i++; $line = "$i $line"; } else { $color = BOLD . BLUE; next; } $line =~ s,<b>,$bold,g; $line =~ s,</b>,$reset,g; $line =~ s/<[^>]+>//g; print $color . "$line\n" . RESET; } print "\n"; } </code> ===== awk + sed 簡單版 ===== <code> #!/bin/sh curl -sd "p=$1" http://tw.dictionary.yahoo.com/search | \ awk "/pexplain/{ print } \ /peng/{ print } \ /pchi/{ print ; print \"\n\" }" | \ sed -e :a -e 's/<[^>]*>//g;/</N;//ba' </code> ===== 改變檔案屬性 ===== <code> $ chmod +x filename </code> 執行: <code> $ ./filename [單字] </code>
perl/yahoo_dictionary.1168585444.txt.gz
· 上一次變更: 2007/04/15 02:41 (外部編輯)
頁面工具
顯示頁面
舊版
反向連結
回到頁頂