close

情境說明:
假設今天我們有很多單字不懂
可是一個一個去查詢又很費時....

現在
我們可以將那些不懂的單字打在檔案裡(D:\NewWords.txt)

然後使用以下的函數(YahooDic_V2)查詢
最後再將查詢的結果儲存成檔案(D:\Result.txt)
以利閱讀與列印

如此
不僅可以快速的查詢到不懂的單字
更重要的是
省下來的時間還可以多背更多的單字!!


使用方法:
PS C:\>get-content D:\NewWords.txt | YahooDic_V2 | out-file D:\Result.txt
#將檔案導到此函數後輸出成檔案

結果


函數內容:

function YahooDic_V2
{
#初始儲存最後結果陣列
$Result=@()
#逐字查詢
foreach($word in $input){
$Result=$Result+"-------------------------------------"+$word+"-------------------------------------"
#使用Net.webclient連網頁
$wc =new-object net.webclient
#設定網頁的編碼---->超級重要
$wc.Encoding=[System.Text.Encoding]::UTF8
#下載結果網頁
$wc.DownloadString("http://tw.dictionary.yahoo.com/search?ei=UTF-8&p=$word") | out-file d:\dic.txt
$c = (get-content D:\dic.txt | out-string) -replace '[\t\n\r\f]+'
$c=$c+"<div class=pcixin>"
$type=@()
$flag=$c -match '<div class=pcixin>(.*?)</div>(.*)'
#解析網頁結果
while($flag)
{
 $type=$type+$matches[1]
 $rest = $matches[2]
 $flag2 = $rest -match '<div class=pexplain>(.*?)</div>(.*?)<div class=pcixin>(.*)'
 $rest2 = $matches[2]
 $count=0
 while($flag2)
{
$type=$type+(" "+[string]('{0:d2}' -f ++$count)+"."+$matches[1])
$flag2 = $rest2 -match '<div class=pexplain>(.*?)</div>(.*)'
$rest2 = $matches[2]
}

$flag=$rest -match '<div class=pcixin>(.*?)</div>(.*)'
}
$Result=$Result+$type
}

#輸出結果
$Result
}

#注意
目前的解析程式完全倚賴Yahoo字典的網頁結構
因此當該網頁結構有變動
則本程式可能無法正常運作
這是一般解析程式最容易遇到的問題!
arrow
arrow
    全站熱搜

    ayowu 發表在 痞客邦 留言(0) 人氣()